Vb.net-nested-if-statements

提供:Dev Guides
移動先:案内検索

VB.Net-ネストされたIfステートメント

VB.Netでは、If-Then-Elseステートメントをネストすることは常に有効です。つまり、1つの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