Valhalla Legends Forums Archive | Visual Basic Programming | Case as to If?

AuthorMessageTime
ObsidianWolf
I have seen tons of code in my few years of coding and more then not people use If instead of Case, when Case would make things a bit more organized. What do you think?
November 26, 2003, 4:18 PM
Spht
You generally should use the If statement if there's a two-way comparison (yes or no; if or else). Use Select Case when you want to run multiple checks. I never use "ElseIf." Example:

[code]Select Case Number
Case 1
Case 2
Case 3
End Select[/code]

Don't do,

[code]If Number = 1 Then
ElseIf Number = 2 Then
ElseIf Number = 3 Then
End If[/code]

This requires more CPU cycles as opposed to a Select Case.
November 26, 2003, 4:29 PM
iago
Yeah, like what spht said, Case is much quicker, since it's a single jmp using a table, but it can't be used to compare numbers (<, >, etc.).
November 26, 2003, 6:39 PM
Skywing
[quote author=iago link=board=31;threadid=3866;start=0#msg31686 date=1069871984]
Yeah, like what spht said, Case is much quicker, since it's a single jmp using a table, but it can't be used to compare numbers (<, >, etc.).
[/quote]
This is probably not (always?) true for VB, because you can use Case on stuff like strings and soforth which would invalidate the optimizations possible with a C switch.
November 26, 2003, 6:54 PM
iago
That's true. I have no idea how it's implemented in VB, but at least in C++ it's a lot faster.
November 26, 2003, 7:07 PM
Adron
[quote author=Spht link=board=31;threadid=3866;start=0#msg31659 date=1069864186]
I never use "ElseIf."
[/quote]

I use ElseIf every once in a while. I suppose Select Case could be used, but it don't like using that with nonconstant cases. Example:

[code]
If a Then
ElseIf b Then
ElseIf c Then
ElseIf d Then
Else
End If
[/code]

vs

[code]
Select Case True
Case a
Case b
Case c
Case d
Case Else
End Select
[/code]


November 27, 2003, 7:29 PM
Puzzle
I use Select Case wherever I can, but Select Case is somewhat limited compared to If/Then.
November 29, 2003, 1:57 AM
Spht
[quote author=Puzzle link=board=31;threadid=3866;start=0#msg32322 date=1070071037]
I use Select Case wherever I can, but Select Case is somewhat limited compared to If/Then.
[/quote]

It is? How so?
November 29, 2003, 1:59 AM
Puzzle
Maybe I should rephrase. Its not exactly more limited, but gets really confusing if you would use it to replace a large nested If/Then.
November 29, 2003, 2:04 AM
Grok
Select Case is most excellent for implementing 'AND' and 'NAND' structures. My favorite usage!

[code]' make sure all conditions have been met...
Result = False
Select Case False
Case A = 15
Case B$ = "Check"
Case C > 11.30
Case Else
'all conditions true!
Result = True
End Select
MsgBox "All Conditions have " & IIF(Result, " ", "NOT ") & "been met!"
[/code]
November 29, 2003, 5:07 AM
Adron
Yes, it does work for those, but I find those much easier to understand when structured more english-language-like; "if this and that then bla".
November 30, 2003, 12:30 AM
TheMinistered
[quote author=iago link=board=31;threadid=3866;start=0#msg31686 date=1069871984] ... but it can't be used to compare numbers (<, >, etc.).
[/quote]

Iago, this is not true. Grok even prooved it in his above example where if a specific case was greater than 11.30 then the code under it would be executed. However, I believe grok did make a simple syntax error. In order to do operations like that, you require the help of the 'Is' operator.

[code]
Select Case 100
Case Is > 10
'omg this case is greater than 10!
Case Is < 0
'omg this case is not less than 0!
End Select


[/code]
November 30, 2003, 3:22 AM
CupHead
[quote author=TheMinistered link=board=31;threadid=3866;start=0#msg32547 date=1070162524]
Iago, this is not true. Grok even prooved it in his above example where if a specific case was greater than 11.30 then the code under it would be executed. However, I believe grok did make a simple syntax error. In order to do operations like that, you require the help of the 'Is' operator.

[code]
Select Case 100
Case Is > 10
'omg this case is greater than 10!
Case Is < 0
'omg this case is not less than 0!
End Select


[/code]
[/quote]

I don't think there's any syntax error. If you'll look at Grok's code, you'll see his Select statement is "Select Case False". This means that in order to do a comparison, he needs to use the variable like C > 100 or whatever. If he uses Is, then the statement becomes False > 100 and not the variable C that he wanted compared.
November 30, 2003, 5:52 AM
TheMinistered
Gogo cuphead!
November 30, 2003, 6:08 AM
Fleet-
I was told by "professionals", that if statements were naturally quicker, is this true?
December 10, 2003, 1:32 PM
Grok
There's nothing natural about If statements.
December 10, 2003, 7:23 PM
Skywing
In a language like C or C++ that imposes strict requirements on what you can have for a switch (analog of "select case"), a multiple-selection flow structure is generally much faster than a series of if statements.

This is because the compiler can often optimize the multiple-selection structure to use only one or two compares, despite there being multiple outcomes, something that generally isn't possible with if statements.

Consider the following:

[code]
int i;
// ...
switch(i) {

case 0: // ...
break;
case 1: // ...
break;
case 2: case 3: // ...
break;
case 4: // ...
break;

}
[/code]

The compiler can be clever and turn this into a table lookup that goes something like this in pseudocode:

handlerindex = handlerindextable[i];
handler = handlertable[handlerindex];
goto handler;

With this kind of optimization, there is no need to individually test for outcomes.

Note that because VB allows you to put almost anything into a select case construction, this optimization is probably not as applicable, so the speed difference between an if and a select case may not be as drastic as the speed difference between an if and a switch in C or C++.
December 10, 2003, 7:40 PM
hismajesty
[quote author=Fleet- link=board=31;threadid=3866;start=0#msg34588 date=1071063154]
I was told by "professionals", that if statements were naturally quicker, is this true?
[/quote]

AFAIK no. When I first started in VB I did If statements for everything and was told that Case is better. Where exactly did these "professionals" work? Also, I may be wrong (and probably am) so I too would like to know. :P
December 10, 2003, 8:43 PM
Spht
[quote author=hismajesty link=board=31;threadid=3866;start=15#msg34639 date=1071089009]
[quote author=Fleet- link=board=31;threadid=3866;start=0#msg34588 date=1071063154]
I was told by "professionals", that if statements were naturally quicker, is this true?
[/quote]

AFAIK no. When I first started in VB I did If statements for everything and was told that Case is better. Where exactly did these "professionals" work? Also, I may be wrong (and probably am) so I too would like to know. :P
[/quote]

Work with the logic which I posted and you'll be fine.
December 10, 2003, 8:56 PM
Adron
[quote author=Grok link=board=31;threadid=3866;start=15#msg34618 date=1071084209]
There's nothing natural about If statements.
[/quote]

They sound like natural language!
December 10, 2003, 10:17 PM
Etheran
I thought polymorphism was invented to rid the world of the cases! ;D
December 11, 2003, 12:43 AM
hismajesty
[quote author=Spht link=board=31;threadid=3866;start=15#msg34644 date=1071089798]
[quote author=hismajesty link=board=31;threadid=3866;start=15#msg34639 date=1071089009]
[quote author=Fleet- link=board=31;threadid=3866;start=0#msg34588 date=1071063154]
I was told by "professionals", that if statements were naturally quicker, is this true?
[/quote]

AFAIK no. When I first started in VB I did If statements for everything and was told that Case is better. Where exactly did these "professionals" work? Also, I may be wrong (and probably am) so I too would like to know. :P
[/quote]

Work with the logic which I posted and you'll be fine.
[/quote]

Aye, so I was correct?
December 11, 2003, 2:29 PM

Search