Select Case vs If Then Else (1 Viewer)

Jon

Access World Site Owner
Staff member
Local time
Today, 16:45
Joined
Sep 28, 1999
Messages
7,304
I am currently learning JavaScript, and just came across Switch Case vs If Else. The tutor said he thinks If Else is better, as the code is shorter, but it comes down to personal preference.

What is the current opinion of Select Case vs If Then Else within the VBA developer community?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 09:45
Joined
Oct 29, 2018
Messages
21,358
I agree. Case is more extensible than If/Then. So, for simple logic, use If/Then, else use Case.
 

Minty

AWF VIP
Local time
Today, 16:45
Joined
Jul 26, 2013
Messages
10,354
I agree with @theDBguy and @plog
I responded to a post here where someone had put 6 or 7 nested if , elseif , else statements together and it was unreadable and really difficult (nigh on impossible) to follow the logic.

A select case type statement is so much cleaner and easier to follow and debug.
 

neuroman9999

Member
Local time
Today, 11:45
Joined
Aug 17, 2020
Messages
827
I am currently learning JavaScript, and just came across Switch Case vs If Else. The tutor said he thinks If Else is better, as the code is shorter, but it comes down to personal preference.

What is the current opinion of Select Case vs If Then Else within the VBA developer community?
why are you asking the VBA development community, Jon?

CASE statements and IF,THEN,ELSE statements exist in every language. and the difference between the two, with regard to efficiency, is so marginal, why even care? Is your tutor afraid the world is going to run out of energy? :p If they are, I wouldn't be surprised one bit.
 

Jon

Access World Site Owner
Staff member
Local time
Today, 16:45
Joined
Sep 28, 1999
Messages
7,304
I tended to use Case when I had a larger numbe of examples. To me, it looked clearer and easier to understand when glancing. That is why I was surprised to see this very experienced coding tutor suggest if else.

Here are the JavaScript versions. Maybe they have more syntax in Switch Case vs If Else when compared to the VBA equivalents?

let role = 'guest';
switch (role) {
case 'guest':
console.log('Guest User');
break;
case 'moderator':
console.log('Moderator User');
break;
default:
console.log('Unknown User');
}
//Moss prefers this:
if (role === 'guest') console.log('Guest');
else if (role === 'moderator') console.log('Moderator');
else console.log('Unknown User');

The tutor is this guy: https://codewithmosh.com/

He is an excellent tutor.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 09:45
Joined
Aug 30, 2003
Messages
36,118
I also agree with Plog. I took a couple of Mosh's C# classes; he is a good instructor.
 

neuroman9999

Member
Local time
Today, 11:45
Joined
Aug 17, 2020
Messages
827
let role = 'guest';
switch (role) {
case 'guest':
console.log('Guest User');
break;
case 'moderator':
console.log('Moderator User');
break;
default:
console.log('Unknown User');
}
//Moss prefers this:
if (role === 'guest') console.log('Guest');
else if (role === 'moderator') console.log('Moderator');
else console.log('Unknown User');

as you can see, Jon, break; is necessary to get out of the block. whereas, with the alternative, none of that is used. thus, you way is probably best. less to look at.

by the way, did you know that break and return are brother and sister? =)
 

Jon

Access World Site Owner
Staff member
Local time
Today, 16:45
Joined
Sep 28, 1999
Messages
7,304
I also agree with Plog. I took a couple of Mosh's C# classes; he is a good instructor.
Did you persist wity C#? Or are you building up your various programming languages?
 

neuroman9999

Member
Local time
Today, 11:45
Joined
Aug 17, 2020
Messages
827
how good can he be if he's working for an org that is doing what they're doing because they're getting rich off of a massive trend, just like Bob Parsons did? and what they big data warehouse people are doing now? :(


and why does his other page have no SSL on it!? it only costs $80 bucks a freakin year!


and considering that he has the word ""awesome"" in his gitHub repos, I'm guessing that he's not welcome in the corporate world because he has a caring soul, and thus he became a teacher. I see that with millions of people literally. you have to be a complete ass to survive in the corporate realm:


 

pbaldy

Wino Moderator
Staff member
Local time
Today, 09:45
Joined
Aug 30, 2003
Messages
36,118
Did you persist wity C#? Or are you building up your various programming languages?

My employer suggested it as they way they wanted to move forward. They've since backed off that path. I'll probably try to keep learning, but for me it's harder without a project to apply it to.
 

Jon

Access World Site Owner
Staff member
Local time
Today, 16:45
Joined
Sep 28, 1999
Messages
7,304
I was learning Ruby on Rails. But recent things I've read are questioning whether or not I am on the right path. So, I am considering a pivot to learning JavaScript, Django and Python. I went to Google Trends to do some reasearch.

1603466425225.png


Source: https://trends.google.com/trends/explore?cat=5&date=all&q=node.js,ruby on rails,django

1603466459551.png


Source: https://trends.google.com/trends/explore?cat=5&date=all&q=ruby,python,react

The relative decline in the use of Ruby on Rails suggested to me that perhaps the best path would be Python + Django. Besides, what I have learned about Ruby on Rails so far (besides my previous programming experience), tells me it is mostly a question of mastering the different syntax. The principles are similar.
 

Isaac

Lifelong Learner
Local time
Today, 09:45
Joined
Mar 14, 2017
Messages
8,738
I agree with everyone else. Sometimes I forget about Select Case and impulsively go to If, then change it back once it starts to get out of hand.
I also like how you can line things up (indents and all) with select case a little easier than If and then ElseIf lines, very trivial perhaps, but I am obsessive about readability.
 

neuroman9999

Member
Local time
Today, 11:45
Joined
Aug 17, 2020
Messages
827
The relative decline in the use of Ruby on Rails suggested to me that perhaps the best path would be Python + Django.

No one cares about Ruby john. You won't get any work learning it. Django is heavily used by a lot of people however the elite people do not use it. They use their own stuff proprietary
 

Jon

Access World Site Owner
Staff member
Local time
Today, 16:45
Joined
Sep 28, 1999
Messages
7,304
OMG I just got stuck in a nasty for loop that froze up my PC. Then I had to switch computers to find out how to escape it! I blew threw half an hour wasting time on a solution!

JavaScript:
for (let i = 1; i = 5; ++i) {
  console.log('Hello World', i);
}

Next, my computer froze and rebooted, I think. I was away from my computer and came back, with the browser saying Restore pages? and Finder closed etc. Still not sure how to escape a code loop if stuck in vs code.
 

Jon

Access World Site Owner
Staff member
Local time
Today, 16:45
Joined
Sep 28, 1999
Messages
7,304
Got stuck in another infinte loop again. Drat! Need to get to a solution for this.

Edit: Found you have to close the tab or Force Quit Chrome. I would have thought there would be some keyboard shortcut to escape a loop. I'm sure there is in VBA?
 

neuroman9999

Member
Local time
Today, 11:45
Joined
Aug 17, 2020
Messages
827
JavaScript:
for (let i = 1; i = 5; ++i) {
  console.log('Hello World', i);
}
ummmm...I believe it's supposed to be:
Code:
for (i = 0; i < 6; i++) {
     console.log('Hello World', i);
}
ya super-nice person! LOL

G O O F B A L L. quit sensoring my stuff!
 

Users who are viewing this thread

Top Bottom