[RESOLVED] Cases and If Statements
Okay, so I've been wondering what exactly the difference between using cases and if statements are. It seems to me that they do exactly the same thing, but I'm sure that there are situations where one is better to use than the other -- What would these situations be?
I was wondering if someone could give me an example to show the difference.
Please and Thank you :)
Re: Cases and If Statements
They are more or less the same thing, but a Select Case is more compact, see http://www.homeandlearn.co.uk/NET/nets1p21.html
Re: Cases and If Statements
If you have lots of possibilities that need to be evaluated, Select Case is much easier to read.
The difference between the two is something for the programmer. The end user could care less.
Re: Cases and If Statements
Quote:
Originally Posted by
Hack
If you have lots of possibilities that need to be evaluated, Select Case is much easier to read.
The difference between the two is something for the programmer. The end user could care less.
Yeah that's one thing I noticed. I wasn't sure if the two acted the same way and it seems like they do.
VB.NET Code:
if '....then
'...
elseif '....then
'...
elseif '....then
'...
else '....then
'...
end if
...does look sloppy ;)
Thanks for the help :)
Re: [RESOLVED] Cases and If Statements
Select case is most useful when checking a single value, Whereas the If/Elsif is more appropriate for checking multiple conditions:
If IsMonday then
' Do the Laundry
ElseIf FortyHoursOfWorkDone Then
' Play Golf
ElseIf ComputerBroken Then
' Play Golf!
Else
' Do Work :(
EndIf
However, even the above can be converted to a Select Case statement, even if it may be a bit hard to read depending on your conditions:
Select Case True
Case IsMonday
Case FortyHoursOfWorkDone
Case ComputerBroken
etc.
Note that VB implicitly exits the case statement when the first match is found. Unfortunately, reading it does not necessarily imply that this is so (particularly if the reader is from a C background). The If/Then may be a better solution, but as already stated, it's programmers choice.