Vbscript-switch-statement

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

VBScriptのSwitchステートメント

ユーザーが式の値に応じてステートメントのグループを実行する場合、Select Caseステートメントを使用できます。 各値は*ケース*と呼ばれ、変数は各ケースに基づいて ON に切り替えられます。 Case Else ステートメントは、テスト式がユーザーが指定したどのケースにも一致しない場合に実行されます。

*Case Else* はSelect Case内のオプションのステートメントです。ただし、Case Elseステートメントを常に保持することは良いプログラミング手法です。

構文

VBScriptのSelectステートメントの構文は次のとおりです-

Select Case expression
   Case expressionlist1
      statement1
      statement2
      ....
      ....
      statement1n
   Case expressionlist2
      statement1
      statement2
      ....
      ....
   Case expressionlistn
      statement1
      statement2
      ....
      ....
  Case Else
      elsestatement1
      elsestatement2
      ....
      ....
End Select

<!DOCTYPE html>
<html>
   <body>
      <script language = "vbscript" type = "text/vbscript">
         Dim MyVar
         MyVar = 1

         Select case MyVar
            case 1
               Document.write "The Number is the Least Composite Number"

            case 2
               Document.write "The Number is the only Even Prime Number"

            case 3
               Document.write "The Number is the Least Odd Prime Number"

            case else
               Document.write "Unknown Number"
         End select
      </script>
   </body>
</html>

上記の例では、MyVarの値は1です。 したがって、ケース1が実行されます。

The Number is the Least Composite Number