VB Case Statement: Usage & Examples
In VB, the Case statement is typically used in conjunction with the Select statement to execute different code blocks based on different conditions. Here is an example:
Select Case score
Case 90 To 100
Console.WriteLine("优秀")
Case 80 To 89
Console.WriteLine("良好")
Case 70 To 79
Console.WriteLine("中等")
Case 60 To 69
Console.WriteLine("及格")
Case Else
Console.WriteLine("不及格")
End Select
In the example above, depending on the value of the variable score, the program will execute different code blocks. If the value of score falls between 90 and 100, it will output “excellent”, and so on.
It is important to note that the Case statement can be used to handle different types of conditions, such as ranges, specific values, expressions, and more. Multiple conditions can also be used in the Case statement, separated by commas.