目录

嵌套的If语句(nested If statements)

在VB.Net中嵌套If-Then-Else语句总是合法的,这意味着你可以在另一个If ElseIf语句中使用一个If或ElseIf语句。

语法 (Syntax)

嵌套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。

例子 (Example)

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
↑回到顶部↑
WIKI教程 @2018