Java foundation's branch statement

Computer from life, the program is simulation of real life, so start with the service life of the teacher's life, and those associated with the program in the world deep roots.

A, if statement structure
1.if basic syntax structure statement:
IF (relational expression) {
basis statement thereof;
}

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入荷包余额:");
        double money = input.nextDouble();

        if(money > 10000){
            System.out.println("请大家吃西餐,--肯德基");
        }
    }

Implementation process:
first determine the relationship between the expression to see the result is true or false;
if the statement is true on the implementation of the body;
if it is false statement is not executed body;

2.if else syntax structure:
IF (relational expression) {
statement body. 1;
} {the else
sentence
2; }

        public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入荷包余额:");
        double money = input.nextDouble();

        if(money > 10000){
            System.out.println("请大家吃西餐,--肯德基");
        }else{
            System.out.println("请大家到饮水机免费打水");
        }

    }

Implementation process
first determines the relationship between the expression to see the result is true or false;
if the statement is true on the implementation of 1
if a false statement is executed 2

3. if multiple syntax structure:
if (relational expression 1) {
statement body 1;
} if the else (the relational expression 2) {
statement body 2;
} ... {the else
statement body 1 + n-;
}

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.println("请输入荷包余额:");
        double money = input.nextDouble();

        //多重if
        if(money > 10000){
            System.out.println("请大家吃西餐,--肯德基");
        }else if(money > 1000){
            System.out.println("请大家喝果汁");
        }else if(money > 100){
            System.out.println("请大家吃棒棒糖");
        }else{
            System.out.println("请大家到饮水机免费打水");
        }
    }

4. nested IF
IF (relational expressions. 1) {
IF (relational expression 2) {
statement body;
} {the else
sentence
thereof;
} }

Two, Switch syntax structure
determination syntax equivalent
switch (expression) {
Case Value 1:
Statement body 1;
BREAK;
Case 2 values:
Statement body 2;
BREAK;
...
defaut:
Statement body 1 + n-;
BREAK;
}

public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int choice = input.nextInt();
        switch(choice){
            case 1:
                System.out.println("吃番茄炒鸡蛋");
                break;
            case 2:
                System.out.println("鸡蛋炒番茄");
                break;
            case 3:
                System.out.println("鸡蛋炒鸡蛋");
                break;
            case 4:
                System.out.println("番茄炒番茄");
                break;
            case 5:
                System.out.println("佛跳墙");
                break;
            case 6:
                System.out.println("红烧狮子头");
                break;
            case 7:
                System.out.println("蚂蚁上树");
                break;
            default:
                System.out.println("今天不吃了");
        }
    }

Elaborate syntax:
behind with the case and is to be an expression value to be compared;
seeing the body part may be one or more statements;
BREAK an interrupt, meaning the end, to the end of the switch statement
default statement indicates that all of the cases are not when matched, on the implementation of the contents of the premises, and else if statements are similar.

Three, IF VS Switch
Switch may be used in the determination of the equivalent time, otherwise employed if;

Published 31 original articles · won praise 4 · Views 3526

Guess you like

Origin blog.csdn.net/qq_29074261/article/details/78753297