if/else and if/elseif

后端 未结 10 1851
傲寒
傲寒 2020-12-17 01:00

If I have a statement block like this:

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

or like this:

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


        
相关标签:
10条回答
  • 2020-12-17 01:32

    Without "elseif" syntax you would have to write chain if-statements for processing one of several possible outcomes this way:

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

    instead you can write

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

    which is completely the same as the previous one, but looks much nicer and is much easier to read.

    0 讨论(0)
  • 2020-12-17 01:32

    Many languages have a grammer like this (here: ECMAScript Language Specification, so JavaScript):

    IfStatement :
        if ( Expression ) Statement else Statement
        if ( Expression ) Statement

    Statement :
        Block
        VariableStatement
        EmptyStatement
        ExpressionStatement
        IfStatement
        IterationStatement
        ContinueStatement
        BreakStatement
        ReturnStatement
        WithStatement
        LabelledStatement
        SwitchStatement
        ThrowStatement
        TryStatement

    Block :
        { StatementListopt }

    StatementList :
        Statement
        StatementList Statement

    So the branches of an ifStatement may contain a block of statements (Block) or one of the other statements (other than Block). That means this is valid:

    if (expr)
        someStatement;
    else
        otherStatement;
    

    And as StatementList may just contain a single statement, these examples are equivalent to the previous:

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

    And when we replace otherStatement by an additional IfStatement, we get this:

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

    The rest is just code formatting:

    if (expr) {
        someStatement;
    } else if (expr) {
        someOtherStatement;
    }
    
    0 讨论(0)
  • 2020-12-17 01:34

    Given a single variable, you will use the simple if-else structure. When there are multiple variables and you have a different path to execute for the different possibilities, you will use if-else if-...-else. Note, that the latter also ends with an else statement.

    0 讨论(0)
  • 2020-12-17 01:35
    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 讨论(0)
  • 2020-12-17 01:39
    **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 and if/else if used also like this

    0 讨论(0)
  • 2020-12-17 01:43

    Situation a:

    if( condition )
    {
    }
    else
    {
    }
    

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

    Situation b:

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

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

    0 讨论(0)
提交回复
热议问题