質問

次のようなステートメントブロックがある場合:

if (/*condition here*/){ }
else{ }

またはこのように:

if (/*condition here*/)
else if (/*condition here*/) {}
else if (/*condition here*/) {}

違いは何ですか?

if / elseの場合、if部分はtrue状態用で、else部分は他のすべての可能なオプション用です(false)。 else-ifは、多くの条件に役立ちます。これは私の理解ですが、他に注意すべきことはありますか?

役に立ちましたか?

解決

状況a:

if( condition )
{
}
else
{
}

上記のステートメントの条件が偽の場合、elseブロックのステートメントは常に実行されます。

状況b:

if( condition )
{
}
else if( condition2 )
{
}
else
{
}

「condition」がfalseの場合、else ifブロックのステートメントは、condition2がtrueの場合にのみ実行されます。 else2のステートメントは、condition2がfalseのときに実行されます。

他のヒント

" elseif"なしこの方法で考えられるいくつかの結果の1つを処理するためのチェーンifステートメントを記述する必要がある構文:

if( str == "string1" ) {
   //handle first case
} else {
    if( str == "string2" ) {
       //handle second case
    } else {
       if( str == "string3" ) {
           //handle third case
       } else {
          //default case
       }
    }
 }

代わりに書くことができます

if( str == "string1" ) {
   //handle first case
} else if( str == "string2" ) {
   //handle second case
} else if( str == "string3" ) {
   //handle third case
} else {
   //default case
}

これは前のものと完全に同じですが、より見栄えがよく、読みやすくなっています。

多くの言語にこのような文法があります(ここで: ECMAScript言語仕様 、したがってJavaScript):

  

IfStatement
        if( Expression ステートメント else ステートメント
        if( Expression ステートメント

     

ステートメント
        ブロック
       VariableStatement
       EmptyStatement
        ExpressionStatement
       IfStatement
       IterationStatement
       ContinueStatement
        BreakStatement
        ReturnStatement
       WithStatement
       LabelledStatement
       SwitchStatement
        ThrowStatement
       TryStatement

     

ブロック
        { StatementList opt }

     

StatementList
        声明
        StatementList Statement

したがって、 ifStatement のブランチには、ステートメントのブロック( Block )または他のステートメントの1つ( Block 以外)が含まれます。 。つまり、これは有効です:

if (expr)
    someStatement;
else
    otherStatement;

また、 StatementList には単一のステートメントが含まれる場合があるため、これらの例は前の例と同等です。

if (expr) {
    someStatement;
} else {
    otherStatement;
}

if (expr)
    someStatement;
else {
    otherStatement;
}

if (expr) {
    someStatement;
} else
    otherStatement;

そして、 otherStatement を追加の IfStatement に置き換えると、次のようになります:

if (expr) {
    someStatement;
} else
    if (expr) {
        someOtherStatement;
    }

残りは単なるコードのフォーマットです:

if (expr) {
    someStatement;
} else if (expr) {
    someOtherStatement;
}

ガンボが言ったことを強調する。

また、言語に本当のelif / elsif / elseif(たとえば、フォーマットによって隠された一種のネストされたチェーンの代わりに" real" else-if命令)がある場合、コンパイラは単一の抽象構文ツリーのノード(または同様、 http://en.wikipedia.org/wiki/Abstract_syntax_treeを参照)ネストする代わりに。

例を挙げます:

C / C ++で言う:

if (a) {
    X
} else if (b) {
    Y
} else if (c) {
    Z
} else {
    0
}

次に、コンパイラは次のようなASTノードを構築します。

   a
  / \
 X   b
    / \
   Y   c
      / \
     Z   0

ただし、選択した言語に実際のif-elseがある場合:

if (a) {
    X
} elif (b) {
    Y
} elif (c) {
    Z
} else {
    0
}

その後、ASTは次のようになりやすくなります。

   (a--b--c)
   /  /  /  \
  X  Y  Z    0

このような言語では、「その他の場合」中括弧が必須でない場合にのみ可能です:

if (a) {
    X
} elif (b) {
    Y
} else if (c) {  // syntax error "missing braces" if braces mandatory
    Z
} else {
    0
}

対応するAST(ブレースが必須でない場合):

   (a--b)
   /  /  \
  X  Y    c
         / \
        Z   0

これにより、CFG-Analysis( http://en.wikipedia.org/wiki/Control_flow_graph 実装の方が簡単です(実際の最適化のメリットはないかもしれませんが、怠hoなプログラマーにとってはメリットがあります:D)。

else if は、基本的に if else 部分が別の if ステートメントであることを意味します。

**if/else**
if(condition)
  statement;
else
   statement;

if / else if / else

if(condition)
 {
   if(condition)
      statement;
   else 
     statement;
  }   
else if(condition)
{
    if(condition)
     statement;
    else
     statement;
}
else
    statement;

if / elseおよびif / else if used this this

単一の変数を指定すると、単純な if-else 構造を使用します。複数の変数があり、異なる可能性に対して異なるパスを実行する場合、 if-else if -...- else を使用します。後者も else ステートメントで終了することに注意してください。

すでに自分で答えを出しました。 if / elseは、int = 2または他の可能なint値のようにtrue / falseの結果を表し、if / elseifは、int = 2、int = 3などのように3つ以上の結果を表します。

また、変数のコンテキストをグループ化します。次のようなすべての結果を確認できます

if (a == 2) { do one thing };
if (a == 3) { do another thing };
...
if (a != 2 && a != 3 ...) { do something else };

if / else / elseifを使用すると、読みやすくなります。

さらに条件を確認したい場合、if..elseifを使用できます。単一の条件であれば、ifまたはif ... elseを使用できます。

ここでは、例とともに完全な説明をアップロードできませんので、次のリンクをご利用ください。

if..elseステートメントの詳細
http://allinworld99.blogspot.in/2016 /02/ifelse-flow-chart-with-easy-example.html
if ... elseifの詳細
http://allinworld99.blogspot.in /2016/02/flow-chart-with-example-for-if-then.html

import java.util.*;

public class JavaApplication21 {
    public static void main(String[] args) {

        Scanner obj = new Scanner(System.in);
        System.out.println("You are watching an example of if & else if statements");

        int choice, a, b, c, d;
        System.out.println(" Enter 1-Addition & 2-Substraction");

        int option = obj.nextInt();
        switch (option) {
            case (1):
                System.out.println("how many numbers you want to add.... it can add up to 3 numbers only");
                choice = obj.nextInt();
                if (choice == 2) {
                    System.out.println("Enter 1st number");
                    a = obj.nextInt();

                    System.out.println("Enter 2nd number");
                    b = obj.nextInt();

                    c = a + b;

                    System.out.println("Answer of adding " + a + " & " + b + " is= " + c);
                } else if (choice == 3) {
                    System.out.println("Enter 1st number");
                    a = obj.nextInt();

                    System.out.println("Enter 2nd number");
                    b = obj.nextInt();

                    System.out.println("Enter 3rd number");
                    c = obj.nextInt();

                    d = a + b + c;

                    System.out.println("Answer of adding " + a + " , " + b + " & " + c + "  is= " + d);
                }
            case (2):
                System.out.println("how many numbers you want to substract.... it can substract up to 3 numbers only");
                choice = obj.nextInt();
                if (choice == 2) {
                    System.out.println("Enter 1st number");
                    a = obj.nextInt();

                    System.out.println("Enter 2nd number");
                    b = obj.nextInt();

                    c = a - b;
                    System.out.println("Answer of substracting " + a + " & " + b + " is= " + c);
                } else if (choice == 3) {
                    System.out.println("Enter 1st number");
                    a = obj.nextInt();

                    System.out.println("Enter 2nd number");
                    b = obj.nextInt();

                    System.out.println("Enter 3rd number");
                    c = obj.nextInt();

                    d = a - b - c;
                    System.out.println("Answer of substracting " + a + " , " + b + " & " + c + "  is= " + d);
                }
            default:
                System.out.println("no option you have chosen" + option);
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top