Branch and loop statements in Java language

One, branch statement

1. Process Control

Classification of flow control statements
(1) Loop structure
The simplest and most basic flow control in the program is executed in sequence according to the sequence of the code.

public class day_004 {
    
    
    public static void main(String[] args){
    
    
        System.out.println("顺");
        System.out.println("序");
        System.out.println("结");
        System.out.println("构");
    }
}

Insert picture description here

2. if statement

Format 1:
if(关系表达式){
语句体;
}
Execution process:
①Calculate the value of the relational expression first;
②If the value of the relational expression is true, execute the statement body, otherwise, it will not execute if it is false;
③Continue to execute the following statement.

public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        int i = 20;
        int j = 10;
        //如果i > j成立,则将j的值赋给i
        if (i > j) {
    
    
            i = j;
        }
        System.out.println("i = " + i);
    }
}

Insert picture description here
Format 2:
if(关系表达式){
语句体1;
}else{
语句体2;
}
Execution process:
①Calculate the value of the relational expression first;
②If the value of the relational expression is true, execute statement body 1, otherwise it will execute statement body 2;
③continue to execute the following statement.

public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        int i = 21;
        //判断i的奇偶性
        if (i % 2 == 0) {
    
    
            System.out.println(i+"是偶数");
        }else{
    
    
            System.out.println(i+"是奇数");
        }
    }
}

Insert picture description here
Format 3:
if(关系表达式1){
语句体1;
}else if(关系表达式2){
语句体2;
}
……
else{
语句体n+1;
}
Execution process:
①Calculate the value of relational expression 1 first;
②If the value of relational expression 1 is true, execute statement body 1, if it is false, calculate the value of relational expression 2 and the value of relational expression 2 If the value is true, execute statement body 2, otherwise, execute relational expression 3,...;
③If no relational expression value is true, execute statement body n+1.

import java.util.Scanner;

public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        //int grade = 97;
        //从键盘输入分数
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入成绩(0-100):");
        int grade = sc.nextInt();
        
        //判断成绩在哪个等级,90-100为A,80-89为B,70-79为C,60-69为D,59及以下为E
        if (grade < 0 || grade > 100) {
    
    
            System.out.println("您输入的分数有误,请重新输入!");
        } else if (grade >= 90 && grade <= 100) {
    
    
            System.out.println("成绩" + grade + "分,等级为A");
        } else if (grade >= 80 && grade < 90) {
    
    
            System.out.println("成绩" + grade + "分,等级为B");
        } else if (grade >= 70 && grade < 80) {
    
    
            System.out.println("成绩" + grade + "分,等级为C");
        } else if (grade >= 60 && grade < 70) {
    
    
            System.out.println("成绩" + grade + "分,等级为D");
        } else {
    
    
            System.out.println("成绩" + grade + "分,等级为E");
        }
    }
}

Insert picture description here

3.switch statement

Format:
switch(表达式){
case 值1:
语句体1;
break;
case 值2:
语句体2;
break;
……
default:
语句体n+1;
[break;]
}
Execution process:
①Calculate the value of the expression first;
②Compare the value of the expression with the value after the case in turn. If there is a corresponding value, the corresponding statement will be executed. During the execution, if a break is encountered End the switch loop;
③If all the case values ​​do not match the value of the expression, execute the statement body in the default, and then end the program.

import java.util.Scanner;

public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        //从键盘输入月份,判断是什么季节
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入月份(0-12):");
        int month = sc.nextInt();
        switch (month) {
    
    
            case 12:
            case 1:
            case 2:
                System.out.println("冬天");
                break;
            case 3:
            case 4:
            case 5:
                System.out.println("春天");
                break;
            case 6:
            case 7:
            case 8:
                System.out.println("夏天");
                break;
            case 9:
            case 10:
            case 11:
                System.out.println("秋天");
                break;
            default:
                System.out.println("您输入的月份有误,请重新输入!");
        }
    }
}

Insert picture description here

Second, the loop statement

The characteristics of the loop structure: there are start and end signs, repeat the same thing.
The composition of the loop structure:
①Initialization statement: indicates the state of the loop beginning; it can be one or more pieces of content to complete a series of initialization operations.
②Condition judgment statement: loop execution condition; use an expression whose result value is boolean type, this expression decides whether to execute the loop body, for example: i>3.
③ Loop body statement: the content executed by the loop; any statement.
④Conditional control statement: represents the content of each change in the execution of the loop; usually a statement is used to change the value of the variable, so as to achieve the effect of controlling whether the loop can continue to execute downward, such as: i++.

1. for loop statement

Format:
for(初始化语句; 条件判断语句; 条件控制语句){
循环体语句;
}
Execution process:
①Execute the initialization statement;
②Execute the conditional judgment statement, if the result is true, execute the loop body statement, otherwise end the loop;
③Execute the conditional control statement;
④Return to ②Continue.

public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        //计算1+2+3+…+98+99+100的值
        int sum = 0;
        for (int i = 0; i < 101; i++) {
    
    
            sum += i;
        }
        System.out.println("从1加到100的值为:" + sum);
    }
}

Insert picture description here

2. While loop statement

Format:
初始化语句;
while(条件判断语句){
循环体语句;
条件控制语句;
}
Execution process:
①Execute the initialization statement;
②Execute the conditional judgment statement, if the result is true, execute the loop body statement, otherwise end the loop;
③Execute the conditional control statement;
④Return to ②Continue.

//用while实现
public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        //计算1+2+3+…+98+99+100的值
        int sum=0;
        int i = 0;
        while (i <= 100) {
    
    
            sum += i;
            i++;
        }
        System.out.println("从1加到100的值为:" + sum);
    }
}

3. do...while loop statement

Format:
初始化语句;
do{
循环体语句;
条件控制语句;
}while(条件判断语句);
Execution process:
①Execute the initialization statement;
②Execute the loop body statement
③Execute the conditional control statement;
④Execute the conditional judgment statement, if the result is true, the execution continues, otherwise the loop ends;
⑤Back to ②Continue.

//用do…while实现
public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        //计算1+2+3+…+98+99+100的值
        int sum=0;
        int i = 0;
        do {
    
    
            sum +=i;
            i++;
        } while (i <= 100);
        System.out.println("从1加到100的值为:" + sum);
    }
}

The difference between the three loops
(1) For loop and while loop first judge whether the condition is established, and then decide whether to execute the loop body;
(2) The do...while loop executes the loop body first, then judge whether the condition is established, and whether to continue executing the loop body.
(3) The difference between for and while:
①In the for loop, the auto-increment variable controlled by the conditional control statement is no longer accessed after the for loop;
②In the while loop, the auto-increment variable controlled by the conditional control statement is in the while loop After the end, you can continue to use it.

Infinite loop format
for( ; ;){ }
while(true){ }
do{ } while(true);

4. Control Jump Statement

Jump control statement
continue : Used in a loop, based on conditional control, skips the execution of a loop body, and continues to the next loop;:
breakused in a loop, based on conditional control, terminates the current entire loop.

5. Loop nesting

The loop statement that contains the loop statement is called loop nesting.

public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        //打印输出九九乘法表
        for (int i = 1; i < 10; i++) {
    
    
            for (int j = 1; j <= i; j++) {
    
    
                System.out.print(j + "*" + i + "=" + (i * j) + '\t');
            }
            System.out.println();
        }
    }
}

Insert picture description here

6. Random

Assignment: Generate a random number
. Steps:
①Guide package
import java.util.Random;
②Create object
Random r = new Random();
③Get random number
int number =r.nextInt();

import java.util.Random;

public class day_004 {
    
    
    public static void main(String[] args) {
    
    
        //获取5个随机数
        Random r = new Random();
        //获取数据的范围,[0,5)
        for (int i = 0; i < 5; i++) {
    
    
            int number = r.nextInt(5);
            System.out.println(number);
        }
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44892517/article/details/113714494