VB.Net - 嵌套 If 语句



在 VB.Net 中,嵌套 If-Then-Else 语句始终是合法的,也就是说可以在另一个 If ElseIf 语句内使用一个 If 或 ElseIf 语句。

语法

嵌套 If 语句的语法如下所示 -

If( boolean_expression 1)Then
   'Executes when the boolean expression 1 is true 
   If(boolean_expression 2)Then
         'Executes when the boolean expression 2 is true 
   End If
End If

你嵌套 If 语句的方式与嵌套 ElseIf...Else 的方式类似。

示例

Module decisions
   Sub Main()
      'local variable definition
      Dim a As Integer = 100
      Dim b As Integer = 200
      ' check the boolean condition 
      
      If (a = 100) Then
         ' if condition is true then check the following 
         If (b = 200) Then
            ' if condition is true then print the following 
            Console.WriteLine("Value of a is 100 and b is 200")
         End If
      End If
      Console.WriteLine("Exact value of a is : {0}", a)
      Console.WriteLine("Exact value of b is : {0}", b)
      Console.ReadLine()
   End Sub
End Module

当以上代码被编译并执行后,会生成以下结果 -

Value of a is 100 and b is 200
Exact value of a is : 100
Exact value of b is : 200
vb.net_decision_making.htm
广告