-
Select case versus If statement
Hi all,
I am not really a fan of if statements. I think they should only be used when comparing different values like this:
Code:
Dim status as boolean = false
Dim number as integer = 5
if status.equals(true) then
elseif number = 5 then
else
end if
However, people say that if should also be used if you don't have more then 2 options.
For example:
Code:
Dim status as boolean = false
if status.equals(true) then
msgbox("true")
else
msgbox("false")
end if
I would rather use:
Code:
Select case status
case true: msgbox("true")
case else: msgbox("false")
or msgbox on a different line whatever you want....
What is this rule about that select case should only be used if there more then 2-3 options. Is it wrong to use a select case instead?
-
Re: Select case versus If statement
It may boil down to one's preference, with many conditions SELECT CASE tends to be more readable (to me) and in cases of two conditions then IF ELSE is fine.
-
Re: Select case versus If statement
I tend to use Select Case a lot when it comes to this stuff, but when it's only two items and if the first condition is false, the second condition is assumed to be true then I'll use an
If Then
Else
End If
structure
-
Re: Select case versus If statement
In my opinion, using If/Else is much more intuitive. You can read it almost as normal english:
Code:
If (some condition is true) Then
do this
Else
do that
End If
The same cannot be said for select case as easily:
Code:
Select Case (some condition)
Case True
do this
Case False
do that
End Select
It's just personal preference though... I wouldn't be surprised even if it compiles to the same code in the end (but it probably doens't).
-
Re: Select case versus If statement
To find a performance difference between two If statements and the corresponding Select statement, you'd probably need to use a multimedia timer or perform both operations several million times in a row. The difference in performance is negligible so I would say it is a matter of personal preference.
Having said that, there really is a difference between If and Select. The Select statement is normally translated to the switch IL instruction which is very efficient. So for the code:
VB Code:
Public Enum testEnum
enum1 = 0
enum2
enum3
End Enum
Public Sub test()
Dim s As testEnum = testEnum.enum1
Select Case s
Case testEnum.enum1
s = testEnum.enum2
Case testEnum.enum2
s = testEnum.enum3
Case testEnum.enum3
s = testEnum.enum1
End Select
End Sub
the IL for sub test() is:
Code:
.method public static void test() cil managed
{
// Code size 33 (0x21)
.maxstack 1
.locals init ([0] valuetype recursor.Module1/testEnum s,
[1] valuetype recursor.Module1/testEnum VB$t_i4$L0)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: switch (
IL_0016,
IL_001a,
IL_001e)
IL_0014: br.s IL_0020
IL_0016: ldc.i4.1
IL_0017: stloc.0
IL_0018: br.s IL_0020
IL_001a: ldc.i4.2
IL_001b: stloc.0
IL_001c: br.s IL_0020
IL_001e: ldc.i4.0
IL_001f: stloc.0
IL_0020: ret
} // end of method Module1::test
However, if you use an expression which is not a boolean or an enumeration, the compiler will generate a series of If statements that will be exactly the same as you were coding a series of If statements yourself. Likewise, if you arbitrarily change the values of your enumerations, the switch statement will be altered. So for the code:
VB Code:
Public Enum testEnum
enum1 = 0
enum2 = 2 '<<< Specifically assign value.
enum3 = 5 '<<< Specifically assign value.
End Enum
Public Sub test()
Dim s As testEnum = testEnum.enum1
Select Case s
Case testEnum.enum1
s = testEnum.enum2
Case testEnum.enum2
s = testEnum.enum3
Case testEnum.enum3
s = testEnum.enum1
End Select
End Sub
the following IL is generated:
Code:
.method public static void test() cil managed
{
// Code size 45 (0x2d)
.maxstack 1
.locals init ([0] valuetype recursor.Module1/testEnum s,
[1] valuetype recursor.Module1/testEnum VB$t_i4$L0)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: switch (
IL_0022,
IL_002c,
IL_0026,
IL_002c,
IL_002c,
IL_002a)
IL_0020: br.s IL_002c
IL_0022: ldc.i4.2
IL_0023: stloc.0
IL_0024: br.s IL_002c
IL_0026: ldc.i4.5
IL_0027: stloc.0
IL_0028: br.s IL_002c
IL_002a: ldc.i4.0
IL_002b: stloc.0
IL_002c: ret
} // end of method Module1::test
Note that the switch statement switches for six cases because the compiler knows that the enumeration starts at 0 and ends at 5.
-
Re: Select case versus If statement
Quote:
In my opinion, using If/Else is much more intuitive. You can read it almost as normal english
Same here, I find Select Case statements horrible to look at and read for some reason..
-
Re: Select case versus If statement
I like using Select Case like this when I want specific conditions.
vb Code:
Select Case True
Case j = 5 AndAlso i = 6
'code...
Case j = 6 AndAlso i = 5
End Select
Especially with long AndAlso lines, it can be very hard to read when using If.
-
Re: Select case versus If statement
I tend to use Select Case in this situation:
Code:
Select Case x
Case 0 To 5, 9, 12 To 20
Case Else
End Select
Using If, that would be very hard to read
Code:
If (x >= 0 AndAlso x <= 5) OrElse x = 9 OrElse (x >= 12 AndAlso x <= 20) Then
Else
End If
-
Re: Select case versus If statement
Hey,
As has already been mentioned, it comes down to personal preference.
If I have two conditions, i.e. true or false, then to me, this is a natural fit for If/Else. However, if there are a number of conditions that need to be checked, then I would default to a Select Statement.
Gary
-
Re: Select case versus If statement
I usually prefer one liners where ever possible. I feel it easier to maintain it that way as I have lesser code to deal with. Don't know about performance implications though.
So Instead of this:
Code:
Dim status as boolean = false
if status.equals(true) then
msgbox("true")
else
msgbox("false")
end if
or this:
Code:
Select case status
case true: msgbox("true")
case else: msgbox("false")
or msgbox on a different line whatever you want....
I would prefer:
vb.net Code:
MsgBox(If(Status, "True", "False"))
'or
If Status Then MsgBox("True") Else MsgBox("False")
'etc.
-
Re: Select case versus If statement
Quote:
Originally Posted by
Pradeep1210
I usually prefer one liners where ever possible. I feel it easier to maintain it that way as I have lesser code to deal with. Don't know about performance implications though.
So Instead of this:
Code:
Dim status as boolean = false
if status.equals(true) then
msgbox("true")
else
msgbox("false")
end if
or this:
Code:
Select case status
case true: msgbox("true")
case else: msgbox("false")
or msgbox on a different line whatever you want....
I would prefer:
vb.net Code:
MsgBox(If(Status, "True", "False"))
'or
If Status Then MsgBox("True") Else MsgBox("False")
'etc.
Why not:
Code:
Messagebox.Show(Status.ToString)
?
-
Re: Select case versus If statement
Hey,
In fairness to Pradeep, I think he was just trying to give a simple example of the technique that he uses.
Gary
-
Re: Select case versus If statement
Quote:
Originally Posted by
JuggaloBrotha
Why not:
Code:
Messagebox.Show(Status.ToString)
?
Yes, that too is ok as long as we need to show "True" and "False" only in the messagebox. I was just trying to get the equivalent of OP's code. :D
-
Re: Select case versus If statement
Quote:
Originally Posted by
gep13
Hey,
In fairness to Pradeep, I think he was just trying to give a simple example of the technique that he uses.
Gary
Yes exactly :thumb:
-
Re: Select case versus If statement
If() new in VS 2010 or something?
-
Re: Select case versus If statement
Quote:
Originally Posted by
minitech
If() new in VS 2010 or something?
VS 2008 actually
-
Re: Select case versus If statement
Quote:
Originally Posted by
minitech
If() new in VS 2010 or something?
Yes.
Then new If() operator behaves like the short-circuited If operators (actually it is ?: in other languages). It evaluates only that part that is required.
VB was lacking this single If() operator that other languages have.
VB already had IIf() function. But it evaluates both the true as well as false conditions. So it was slower (as well as unfit for some cases) than the new If() operator
-
Re: Select case versus If statement
Quote:
Originally Posted by
Pradeep1210
I would prefer:
vb.net Code:
MsgBox(If(Status, "True", "False"))
'or
If Status Then MsgBox("True") Else MsgBox("False")
'etc.
@Pradeep
A bit off topic but.. How do you make your code colored?
-
Re: Select case versus If statement
Hey,
I believe I am correct in saying that this is done using VBCODE tags, rather than standard CODE tags:
Code:
MsgBox(If(Status, "True", "False"))
'or
If Status Then MsgBox("True") Else MsgBox("False")
'etc.
vb.net Code:
MsgBox(If(Status, "True", "False"))
'or
If Status Then MsgBox("True") Else MsgBox("False")
'etc.
VBCODE tags, actually render a HIGHLIGHT tag, which you can provide an argument to, i.e. HIGHLIGHT=vb, or HIGHLIGHT=vb.net
Gary
-
Re: Select case versus If statement
Yep. either select your code and click the VBCode button on the toolbar, that will automatically put the tags for you.
Or you may manually put your code between [HIGHLIGHT=vb.net] [/HIGHLIGHT] tags.
-
Re: Select case versus If statement
Quote:
Originally Posted by
minitech
I like using Select Case like this when I want specific conditions.
vb Code:
Select Case True
Case j = 5 AndAlso i = 6
'code...
Case j = 6 AndAlso i = 5
End Select
Especially with long AndAlso lines, it can be very hard to read when using If.
What is the difference in using Case j=5 And i=6 with Case j=5 AndAlso i=6?
Quote:
Originally Posted by
stanav
I tend to use Select Case in this situation:
Code:
Select Case x
Case 0 To 5, 9, 12 To 20
Case Else
End Select
Using If, that would be very hard to read
Code:
If (x >= 0 AndAlso x <= 5) OrElse x = 9 OrElse (x >= 12 AndAlso x <= 20) Then
Else
End If
Also here, what is the difference between And and AndAlso, and OR and OrElse?
Confused...
-
Re: Select case versus If statement
Quote:
Originally Posted by
doctrin13th
What is the difference in using Case j=5 And i=6 with Case j=5 AndAlso i=6?
Also here, what is the difference between And and AndAlso, and OR and OrElse?
Confused...
Check this: http://social.msdn.microsoft.com/for...a-3815db83e2b3 ....:wave:
-
Re: Select case versus If statement
There is some more discussion on this here:
http://visualbasic.about.com/od/quic...vbnetlogop.htm
As well as the MSDN documentation here:
And
AndAlso
Or
OrElse
Gary
-
Re: Select case versus If statement
Also, about performance.
Even though the difference is minimal, the code will be faster if you provide the expression which is more likely to appear first. For example (applies to both If and Select Case):
Code:
If moreprobablecondition Then
...
Else
...
End If
Or
Code:
Select Case expression
Case moreprobablevalue ' Should come first
Case Else
End Select