Dabai became the eleventh day of the siege lion of Java software (while control statement, do..while control statement, break, continue control loop statement)

Control statements in Java

while loop control statement

1. The grammatical structure of the while loop

	while(布尔表达式){
    
    
		循环体;
	}

2. The execution principle of the while loop:

Judge the result of the Boolean expression, if the result is true, continue to execute the loop body until the result of the Boolean expression is false, and the loop ends.

3. The number of cycles of the while loop: 0~N times

Note: The loop body of the while loop may be executed 0 times.

public class WhileTest01 {
    
    

    public static void main(String[] args){
    
    
    	int i = 10;
    	int j = 3;
    	while(i >j){
    
    
    		System.out.println("死循环");
    	}
    	//编译通过
    	System.out.println("Hello World!");
    	
    	//编译错误
    	/*
    	while(10 > 3){
    		System.out.println("死循环");
    	}
    	*/
    	
    	//编译错误:无法访问语句
    	//System.out.println("Hello World!");
    	}
    }
}

do...while loop control statement

1. Syntax structure of do...while loop

	do{
    
    
		循环体;
	}while(布尔表达式);

2. The execution principle of the do...while loop

The body of the loop is executed first, and then it is judged whether the Boolean representation is true, true is executed, and false the loop ends.

3. The number of executions of the do...while loop

The number of executions of the code fragment of the do...while loop is: 1~N [at least once]

4. Precautions for using do...while loop

The do...while loop statement finally has a "semicolon". Don't lose it.

public class DoWhileTest{
    
    
	public static void main(String[] args){
    
    
		int i=10;
		do{
    
    
			System.out.println(i);
		}while(i>100);  //输出10,先执行在进行判断

		while(i>100){
    
    
			System.out.println("i-->"+i);
		} //无输出
	}
}

break control statement

  • Break is a keyword in the java language, which is translated as "break/break".
  • break + ";" can become a single complete java statement: break;
  • The break statement is used in the switch statement to terminate the execution of the switch statement.
  • The break statement can also be used in loop statements to terminate the execution of the loop.
  • Which loop does break terminate?
  • The break; statement is used in for, while, do...while loop statements to break out of the loop and terminate the execution of the loop. Because when the program loops to a certain condition, the subsequent loops do not need to be executed, and the execution also consumes resources, so the loop can be terminated, which can improve the execution efficiency of the program.
  • By default: the break statement terminates the loop statement closest to it.
  • Of course, you can also specify to terminate a loop, you need to name the loop, using this syntax: break loop name;

The following takes the for loop as an example to explain the break; statement

public class BreakTest{
    
    
	public static void main(String[] args){
    
    
		for(int i=0;i<10;i++){
    
    
			if(i==5)
			break;
		}
			System.out.println(i); //0 1 2 3 4

		for(int j=0;j<3;j++){
    
    
			for(int i=0;i<10;i++){
    
    
				if(i==5)
				break;//这里的break语句终止的是内层for循环,因为这个for离它最近。
				//这里的break语句不会影响到外层的for循环
			}
				System.out.println(i); //0 1 2 3 4    0 1 2 3 4      0 1 2 3 4
		}
		
		//给for循环起名
		for1:for(int j=0;j<3;j++){
    
    
			for2:for(int i=0;i<10;i++){
    
    
				if(i==5){
    
    
					break for1;//终止for1循环
				}
				System.out.println(i); //0 1 2 3 4  
			}
		}
	}
}

continue control statement

1. Continue means: continue /go on/ next

2, continue is also a continue keyword plus a semicolon to form a single complete java statement, which mainly appears in the loop statement to control the execution of the loop.

3. The difference between break and continue:

  • Break means that the loop is not executed.
  • continue means to go directly to the next loop to continue execution.
public class ContinueTest{
    
    
	public static void main(String[] args){
    
    
		for(int i=0;i<10;i++){
    
    
			if(i==5)
			break;
		}
			System.out.println(i); //0 1 2 3 4

		for(int i=0;i<10;i++){
    
    
			if(i==5)
			continue; //只要这个语句执行,当前本次循环停止,直接进入下一次循环“继续”执行
		}
			System.out.println(i); //0 1 2 3 4 6 7 8 9
	}
}

4. Continue also has this syntax:

continue loop name;

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112680183