If / else and if / elseif

If I have a statement block as follows:

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

or like this:

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

What is the difference?

It seems like with if / else if part is for true state and else is for all other possible options (false). Another - if it were useful for a number of conditions. This is my understanding, is there anything else I should know about?

+6
source share
10 answers

Situation a:

if( condition )
{
}
else
{
}

When the condition in the above expression is false, the statements in the else block will always be executed.

Situation b:

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

If "condition" is false, then the instructions in the else if block will be executed only when condition2 is true. The statements in the else block will be executed when condition2 is false.

+14

"elseif" 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
}

, .

+21

(: ECMAScript Language Specification, JavaScript):

IfStatement:
if ( ) else
if ( )

:


EmptyStatement
ExpressionStatement
IfStatement

ContinueStatement
BreakStatement
ReturnStatement


SwitchStatement
ThrowStatement
Try

:
{ StatementList opt }

:

StatementList Statement

, ifStatement (Block) ( 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;
}
+3

, .

, elif/elsif/elseif (, "" else-if , ), node ( , . http://en.wikipedia.org/wiki/Abstract_syntax_tree) .

:

C/++, :

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

AST- node :

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

if-else:

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

:

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

"if else" , :

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- (http://en.wikipedia.org/wiki/Control_flow_graph) (, , , imho it ' : D).

+3

else if , else if - if.

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

/ /

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

/ /, ,

+2

, if-else. , , if-else if-...-else. , else.

0

. if/else true/false, , int = 2 int, /elseif - , int = 2 int = 3 ..

. ,

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

if/else/elseif .

0
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);
        }
    }
}
0

All Articles