Traditional Culture Encyclopedia - Weather forecast - How to use select case statement in VB
How to use select case statement in VB
Select case < represents variable or expression >;
& lt Default statement group, which is executed when the above statement group is not practical due to circumstances >;
End selection
Among them, the key words indicating the situation are IS and TO, and when multiple situations are to be juxtaposed, use commas (English) to connect them. When the Case Else statement is omitted and the statement group is unrealistic due to the situation, the execution of the statement will directly reach the End Select. If you need to skip unnecessary statements and end the Select Case directly when executing statements, please use exit select.
pay attention to
In "",when the keyword is not true or false, the logical operation (AND, OR, NOT) will be meaningless. Because the result of logical operation is true or false, it is not a value or range that represents the specific situation. When you need a larger size, please use the IS and To keywords. The above statement is to give a general grammar.
In "",when it Is True or False, the is and To keywords are meaningless.
Remember not to confuse the above two situations.
nesting
You can also write a complete Select Case statement in each statement group, just like nesting an auxiliary loop in a For loop. Don't get me wrong, Select Case is not a loop statement, but a conditional selection statement, similar to IF ... Ersev ... Ersev ... ending IF.
Basic usage
Dedicated Subtrunk ()
Select Case InputBox ("Please enter the weather you want, and choose one from sunny, cloudy, cloudy, shower, light rain, light snow and hail." "Please enter the weather you want", "Sunny")
Case "Sunny Day"
MsgBox "Wow! You like sunny days. I wish you a beautiful "sunny day" ~! "
Case "cloudy"
MsgBox "Do you like cloudy days because you are afraid of ultraviolet rays?"
Case "cloudy day"
MsgBox "Why do you like cloudy days? How curious ~! "
Case "Shower"
MsgBox "Why do you like rainy days? How curious ~! "
Case "Xiaoyu"
MsgBox "Why do you like rainy days? How curious ~! "
Case "Light Snow"
MsgBox "Why do you like snowy days? How curious ~! "
Case "Hail"
MsgBox "Why do you like hail days? How curious ~! "
Other cases
"MsgBox" can't be purple! I can only choose one for you! "
End selection
End joint
The keyword used is
Dedicated Subtrunk ()
Select Case Day (now)
The case is<= 10.
MsgBox "The first half of this month!"
The case is & gt20
MsgBox "It's already the second half of this month!"
Other cases
MsgBox "It's the middle of this month!"
End selection
End joint
& lt the above code passed the VB6.0 test >
Use keywords to
Dedicated Subtrunk ()
Select case workday (now)
Case 1, 7
MsgBox "Today is the weekend!"
Cases 2 to 6
MsgBox "Today is not the weekend!"
End selection
End joint
& lt the above code passed the VB6.0 test >
The keywords IS and TO can be used together in the same situation as needed, for example
The cases are< = 5,8 to 10, Is & gt20.
However, it should be noted that the interval represented by TO is a closed interval, which is not applicable to an open interval (or a semi-closed and semi-open interval). If you must use select Case to determine the range of values, you can do this:
Dedicated Subtrunk ()
Go to error when error occurs.
Dim num As Single
Num = InputBox ("Please enter!" )
Select case number
The case is<= 1
MsgBox "This number does not exceed 1"
The case is< three
MsgBox "This number is in the open range between 1 and 3"
The case is< five
MsgBox "This number is in the semi-closed and semi-open range between 3 and 5"
The case is < = 7.
MsgBox "The number is in the closed range between 5 and 7"
The case is> Seven
MsgBox "This number is greater than 7"
End selection
outlet connection
Error:
MsgBox "This is not a comparable figure"
End joint
At this point, each case can't switch the order casually. Although this can achieve the desired effect, it worries me because it is not rigorous enough. So what is the most rigorous? See advanced usage.
Advanced usage
Grammar (here is just for a better understanding, change the expression slightly, without changing the real meaning. )
Select Case true.
Case & lt logical operation expression1>;
& lt statement group1>;
Case < logical operation expression 2 >;
& lt statement group 2 >;
....
Case & lt logical operation expression n>
& lt statement group n>
Other cases
& lt Default statement group, which is executed when any one of the above logical operations is False >;
End selection
Under thIs grammar, the keywords Is and To are meaningless, and the value of is is true (is it a variable? This question is really memorable. )
As long as the operation result of the logical operation expression is true, the statement group in this case is executed. Then, the above example will be changed.
Dedicated Subtrunk ()
Go to error when error occurs.
Dim num As Single
Num = InputBox ("Please enter!" )
Select Case True.
Case number & gt= 5 and num<= 7.
MsgBox "The number is in the closed range between 5 and 7"
Case numbers & lt3 and num> 1
MsgBox "This number is in the open range between 1 and 3"
Case number & gt Seven
MsgBox "This number is greater than 7"
Case numbers & lt5 and num>= 3.
MsgBox "This number is in the semi-closed and semi-open range between 3 and 5"
End selection
outlet connection
Error:
MsgBox "This is not a comparable figure"
End joint
As you can see, I changed the order at will and passed the test. So, I found another question that I love to ponder: can the case selection really be changed to the case selection holiday? The answer is yes. I won't give examples here, just grammar.
Error in selecting case
Case & lt logical operation expression1>;
& lt statement group1>;
Case < logical operation expression 2 >;
& lt statement group 2 >;
....
Case & lt logical operation expression n>
& lt statement group n>
Other cases
& lt Default statement group, which is executed when any of the above logical operations is true >;
End selection
As long as the operation result of the logical operation expression is false, the statement group in this case is executed.
- Related articles
- Shuangliu 15 weather forecast
- Make sentences gradually
- 20 168 weather conditions in sanya.
- Fifteen-day weather forecast in Yongding
- Huawei's new mobile phone alarm clock game, have you seen a talking alarm clock?
- What clouds are sunny?
- When is the best season in Switzerland?
- What does the value on the sunscreen mean?
- Fun activity plan
- When is the best time to travel to Hainan?