• Welcome to Valhalla Legends Archive.
 

Case as to If?

Started by ObsidianWolf, November 26, 2003, 10:18 AM

Previous topic - Next topic

What do you prefer using within a program?  If or Select Case?

If
1 (5%)
Select Case
8 (40%)
Each in its own place
11 (55%)

Total Members Voted: 13

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?

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:

Select Case Number
   Case 1
   Case 2
   Case 3
End Select


Don't do,

If Number = 1 Then
ElseIf Number = 2 Then
ElseIf Number = 3 Then
End If


This requires more CPU cycles as opposed to a Select Case.

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.).  
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Skywing

Quote from: iago on November 26, 2003, 12:39 PM
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.).  
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.

iago

That's true.  I have no idea how it's implemented in VB, but at least in C++ it's a lot faster.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Adron

Quote from: Spht on November 26, 2003, 10:29 AM
I never use "ElseIf."

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:


If a Then
ElseIf b Then
ElseIf c Then
ElseIf d Then
Else
End If


vs


Select Case True
Case a
Case b
Case c
Case d
Case Else
End Select




Puzzle

I use Select Case wherever I can, but Select Case is somewhat limited compared to If/Then.

Spht

Quote from: Puzzle on November 28, 2003, 07:57 PM
I use Select Case wherever I can, but Select Case is somewhat limited compared to If/Then.

It is? How so?

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.

Grok

Select Case is most excellent for implementing 'AND' and 'NAND' structures.  My favorite usage!

'  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!"


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".

TheMinistered

#11
Quote from: iago on November 26, 2003, 12:39 PM... but it can't be used to compare numbers (<, >, etc.).

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.


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



CupHead

Quote from: TheMinistered on November 29, 2003, 09:22 PM
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.


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




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.

TheMinistered

#13
Gogo cuphead!

Fleet-

I was told by "professionals", that if statements were naturally quicker, is this true?