Visual Basic 嵌套 If 语句
在 VB 支持 If-Then-Else 语句,这意味着可以在一个 If ElseIf 语句中使用另一个 If 或 ElseIf 语句。
语法
嵌套的 If 语句的语法如下:
If( boolean_expression 1)Then'当 boolean_expression 1 为 true 时执行If(boolean_expression 2)Then'当 boolean_expression 2 为 true 时执行End IfEnd If
嵌套 ElseIf…Else 的方式与嵌套 If 语句的方式类似。
实例
Module decisionsSub Main()'local variable definitionDim a As Integer = 100Dim b As Integer = 200' check the boolean conditionIf (a = 100) Then' if condition is true then check the followingIf (b = 200) Then' if condition is true then print the followingConsole.WriteLine("Value of a is 100 and b is 200")End IfEnd IfConsole.WriteLine("Exact value of a is : {0}", a)Console.WriteLine("Exact value of b is : {0}", b)Console.ReadLine()End SubEnd Module
结果如下:
Value of a is 100 and b is 200Exact value of a is : 100Exact value of b is : 200