Vbscript-nested-if-statements

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

VBScriptのネストされたIfステートメント

別の If または ElseIf ステートメント内の If または ElseIf ステートメント。 Inner Ifステートメントは、最も外側の If ステートメントに基づいて実行されます。 これにより、VBScriptは複雑な条件を簡単に処理できます。

構文

VBScriptのネストされたif文の構文は-

If(boolean_expression) Then
   Statement 1
    .....
    .....
   Statement n
   If(boolean_expression) Then
      Statement 1
        .....
        .....
      Statement n
   ElseIf (boolean_expression) Then
      Statement 1
        .....
        ....
      Statement n
   Else
       Statement 1
        .....
        ....
       Statement n
   End If
Else
   Statement 1
    .....
    ....
   Statement n
End If

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim a
         a = 23

         If a > 0 Then
            Document.write "The Number is a POSITIVE Number"
            If a = 1 Then
               Document.write "The Number is Neither Prime NOR Composite"
            Elseif a = 2 Then
               Document.write "The Number is the Only Even Prime Number"
            Elseif a = 3 Then
               Document.write "The Number is the Least Odd Prime Number"
            Else
               Document.write "The Number is NOT 0,1,2 or 3"
            End If
         ElseIf  a < 0 Then
            Document.write "The Number is a NEGATIVE Number"
         Else
            Document.write "The Number is ZERO"
         End If
      </script>
   </body>
</html>

上記のコードが実行されると、次の結果が生成されます-

The Number is a POSITIVE Number
The Number is NOT 0,1,2 or 3