Can someone explain the command "case" better to me? Is it better then if,else,then? Show a example. I have a class called introduction to programming design and im trying to jump ahead but the book doesnt explain it clear enough. Can you help?
Printable View
Can someone explain the command "case" better to me? Is it better then if,else,then? Show a example. I have a class called introduction to programming design and im trying to jump ahead but the book doesnt explain it clear enough. Can you help?
It's used in the Select Case statement. Select Case is used to choose between different results.
Code:Retval = InputBox("Enter your age")
Select Case Retval
Case 20
MsgBox "You are the same age as me"
Case 80
MsgBox "You are older than my grandparents"
Case Is < 12
MsgBox "You are still a kid"
Case Else
MsgBox "Your age does not fit this criteria"
End Select
Select Case is similar to the 'if..then..else' construct, but I like Case better for 2 reasons.
It is more readable (especially if there are many possibilities).
code:
Select Case (month)
Case "January"
'do something
Case "February"
'do something else
'etc...
End Select
is more readable than
If (month = "January") then
'do something
Else
If (month = "February") then
'etc...
end if
Also I understand that Select Case is better performance wise. something about the number of comparisons?
I'm new to VB myself so correct me if I'm wrong.
So its just like the enlgish term "In case"