Java learning road-control statement

Java learning road-control statement

One, conditional statement

1. if … else …

grammar

if(布尔表达式)
{
    
    
   //如果布尔表达式为true将执行的语句
}

example

import java.util.Scanner;

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        int num = input.nextInt();

        if (num >= 0) {
    
    
            System.out.println("输入了一个非负数");
        } else {
    
    
            System.out.println("输入了一个负数");
        }
    }
}

2. if … else if … else …

grammar

if(布尔表达式 1){
    
    
   //如果布尔表达式 1的值为true执行代码
} else if(布尔表达式 2){
    
    
   //如果布尔表达式 2的值为true执行代码
} else {
    
    
   //如果以上布尔表达式都不为true执行代码
}

example

import java.util.Scanner;

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个整数:");
        int num = input.nextInt();

        if (num > 0) {
    
    
            System.out.println("输入了一个非负数");
        } else if (num < 0) {
    
    
            System.out.println("输入了一个负数");
        } else {
    
    
            System.out.println("输入了一个 0");
        }
    }
}

Two, loop statement

1. for loop

grammar

for(初始化; 布尔表达式; 更新) {
    
    
    //代码语句
}

Implementation process

  • Perform the initialization step first. One type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement;
  • Then, check the value of the Boolean expression. If true, the loop body is executed. If it is false, the loop is terminated and the statement following the loop body is executed;
  • After executing a loop, update the loop control variable;
  • Check the Boolean expression again, and execute the above process in a loop.

example

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        for (int i=0 ; i<10 ; i++) {
    
    
            System.out.println(i);
        }
    }
}

2. Enhance the for loop

grammar

for(声明语句 : 表达式)
{
    
    
   //代码句子
}

**Declaration statement: **Declare a new local variable, the type of the variable must match the type of the array element. Its scope is limited to the loop statement block, and its value is equal to the value of the array element at this time.

**Expression: **Expression is the name of the array to be accessed, or a method that returns an array.

example

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int[] nums = {
    
    0, 1, 2, 3};

        for (int i:nums) {
    
    
            System.out.println(i);
        }
    }
}

3. While loop

grammar

while( 布尔表达式 ) {
    
    
  //循环内容
}

** Execution process: ** As long as the Boolean expression is true, the loop will continue to execute.

example

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int i = 0;

        while (i < 10) {
    
    
            System.out.println(i);
            i++;
        }
    }
}

4. do… while loop

grammar

do {
    
    
       //代码语句
} while (布尔表达式);

** Execution process: ** The Boolean expression is after the loop body, so the statement block has been executed before the Boolean expression is detected. If the value of the Boolean expression is true, the statement block is executed until the value of the Boolean expression is false.

example

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int i = 0;

        do {
    
    
            System.out.println(i);
            i++;
        } while (i < 10);
    }
}

5. break 与 continue

use

  • break: Mainly used in loop statements or switch statements to jump out of the entire statement block. Break only jumps out of the innermost loop, and the outer loop will continue to execute.
  • continue: Applicable to any loop control structure. The effect is to make the program immediately jump to the next iteration of the loop. In the for loop, the continue statement causes the program to immediately jump to the update statement. In the while or do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.

example

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        for (int i = 0 ; i<10 ; i++) {
    
    
            if (i % 2 == 0) {
    
    
                continue;
            }
            System.out.println(i);
            if (i == 9) {
    
    
                break;
            }
        }
    }
}

6. Labeled break and continue

The break statement and continue statement will have an effect on the most recent loop by default. If we want to have an effect on the specified loop, we can achieve the effect by adding labels.

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        label:for (int i=0 ; i<10 ; i++) {
    
    
            for (int j=0; j<10 ; j++) {
    
    
                if (j % 2 == 1) {
    
    
                    System.out.println(j);
                    continue label;
                }
            }
        }
    }
}

After adding the label, the break statement and continue statement can end the specified loop statement.

Three, branch selection statement

The switch case statement determines whether a variable is equal to a certain value in a series of values, and each value is called a branch.

grammar

switch(expression){
    
    
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}

The switch case statement has the following rules:

  • The variable type in the switch statement can be byte, short, int or char. Starting from Java SE 7, switch supports the String type, and the case label must be a string constant or literal.
  • The switch statement can have multiple case statements. Each case is followed by a value to be compared and a colon.
  • The data type of the value in the case statement must be the same as the data type of the variable, and it can only be a constant or a literal constant.
  • When the value of the variable is equal to the value of the case statement, the statement after the case statement starts to execute, and the switch statement will not jump out until the break statement appears.
  • When a break statement is encountered, the switch statement terminates. The program jumps to the execution of the statement following the switch statement. The case statement does not have to include the break statement. If no break statement appears, the program will continue to execute the next case statement until a break statement appears.
  • The switch statement can contain a default branch, which is generally the last branch of the switch statement (it can be in any position, but it is recommended to be the last branch). default is executed when there is no case statement and the variable value is equal. The default branch does not require a break statement.
  • When the switch case is executed, it will be matched first, the value of the current case will be returned if the match is successful, and then judge whether to continue output or jump out of judgment according to whether there is a break.
  • If there is no break statement in the case statement block, the JVM will not output the return value corresponding to each case in sequence, but will continue to match. If the match fails, the default case will be returned.
  • If there is no break statement in the currently matched case statement block, starting from the current case, all subsequent case values ​​will be output. If the subsequent case statement block has a break statement, the judgment will be jumped out

example

import java.util.Scanner;

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        Scanner input = new Scanner(System.in);
        System.out.print("请输入你的成绩:");
        int score = input.nextInt();

        switch (score % 10) {
    
    
            case 9:
                System.out.println("优异");
                break;
            case 8:
                System.out.println("良好");
                break;
            case 7:
                System.out.println("一般");
                break;
            case 6:
                System.out.println("及格");
                break;
            default:
                System.out.println("不及格");
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/110141901