Javascript-ifelse

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

JavaScript-if …​ elseステートメント

プログラムの作成中に、特定のパスのセットから1つを採用する必要がある場合があります。 そのような場合、プログラムが正しい決定を下し、正しいアクションを実行できるようにする条件文を使用する必要があります。

JavaScriptは、さまざまな条件に基づいてさまざまなアクションを実行するために使用される条件ステートメントをサポートしています。 ここで、 if..else ステートメントについて説明します。

if-elseのフローチャート

次のフローチャートは、if-elseステートメントの動作を示しています。

意思決定

JavaScriptは if..else ステートメントの次の形式をサポートしています-

  • ifステートメント
  • if …​ elseステートメント
  • if …​ else if …​ ステートメント。

ifステートメント

*if* ステートメントは、JavaScriptが決定を行い、ステートメントを条件付きで実行できるようにする基本的な制御ステートメントです。

構文

基本的なif文の構文は次のとおりです-

if (expression) {
   Statement(s) to be executed if expression is true
}

ここでは、JavaScript式が評価されます。 結果の値がtrueの場合、指定されたステートメントが実行されます。 式が偽の場合、ステートメントは実行されません。 ほとんどの場合、決定を行う際に比較演算子を使用します。

次の例を試して、 if ステートメントの動作を理解してください。

<html>
   <body>
      <script type = "text/javascript">
         <!--
            var age = 20;

            if( age > 18 ) {
               document.write("<b>Qualifies for driving</b>");
            }
        //-->
      </script>
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

出力

Qualifies for driving
Set the variable to different value and then try...

if …​ elseステートメント

*'if ... else'* ステートメントは、JavaScriptがより制御された方法でステートメントを実行できるようにする次の形式の制御ステートメントです。

構文

if (expression) {
   Statement(s) to be executed if expression is true
} else {
   Statement(s) to be executed if expression is false
}

ここでJavaScript式が評価されます。 結果の値がtrueの場合、「if」ブロック内の指定されたステートメントが実行されます。 式が偽の場合、elseブロック内の指定されたステートメントが実行されます。

JavaScriptでif-elseステートメントを実装する方法を学ぶには、次のコードを試してください。

<html>
   <body>
      <script type = "text/javascript">
         <!--
            var age = 15;

            if( age > 18 ) {
               document.write("<b>Qualifies for driving</b>");
            } else {
               document.write("<b>Does not qualify for driving</b>");
            }
        //-->
      </script>
      <p>Set the variable to different value and then try...</p>
   </body>
</html>

出力

Does not qualify for driving
Set the variable to different value and then try...

if …​ else if …​ ステートメント

*if ... else if ...* ステートメントは、 *if…else* の高度な形式であり、JavaScriptがいくつかの条件から正しい決定を下せるようにします。

構文

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

if (expression 1) {
   Statement(s) to be executed if expression 1 is true
} else if (expression 2) {
   Statement(s) to be executed if expression 2 is true
} else if (expression 3) {
   Statement(s) to be executed if expression 3 is true
} else {
   Statement(s) to be executed if no expression is true
}

このコードについて特別なことは何もありません。 これは一連の if ステートメントであり、各 if は前のステートメントの else 句の一部です。 ステートメントは真の条件に基づいて実行され、どの条件も真でない場合、 else ブロックが実行されます。

JavaScriptでif-else-ifステートメントを実装する方法を学ぶには、次のコードを試してください。

<html>
   <body>
      <script type = "text/javascript">
         <!--
            var book = "maths";
            if( book == "history" ) {
               document.write("<b>History Book</b>");
            } else if( book == "maths" ) {
               document.write("<b>Maths Book</b>");
            } else if( book == "economics" ) {
               document.write("<b>Economics Book</b>");
            } else {
               document.write("<b>Unknown Book</b>");
            }
        //-->
      </script>
      <p>Set the variable to different value and then try...</p>
   </body>
<html>

出力

Maths Book
Set the variable to different value and then try...