Java--flow control (loop statement, loop control statement)

1. Loop statement

        A loop statement is to execute a certain operation repeatedly when certain conditions are met. Three commonly used loop statements are provided in Java, namely while loop statement, do...while loop statement and for loop statement.

(1) while loop statement

        The while statement is also called a conditional judgment statement, and its looping method is to use a condition to control whether to continue to execute this statement repeatedly. The syntax is as follows:

while(条件表达式)
{
    执行语句
}

        When the return value of the conditional expression is true, the statement in "{}" is executed. After the statement in "{}" is executed, the return value of the conditional expression is re-evaluated until the result returned by the expression is false , exit the loop. The execution process of the while loop statement is shown in the figure.

[Example] Create the Test01 class in the project, add the integers 1~10 through the while loop in the main method, and output the result.

public class Test01 {
	public static void main(String args[]) {
		int x=1;
		int sum=0;
		while(x<=10) {
			sum=sum+x;
			x++;
		}
		System.out.println("sum="+sum);
	}
}

The running result is shown in the figure:

(2) do...while loop statement 

        The do...while loop statement is similar to the while loop statement. The difference between them is that the while statement first judges whether the condition is true and then executes the loop body, while the do...while loop statement executes the loop first, and then judges the condition Whether it is established. That is to say, the program segment in "{ }" in the do...while loop statement must be executed at least once.

do
{
    执行语句
}
while(条件表达式);

        An obvious difference between the do...while statement and the while statement is that the do...while statement has an extra semicolon (;) at the end, and its execution process is shown in the figure:

 (3) for loop statement

        The for loop is one of the most useful loop statements in Java programming. A for loop can be used to repeatedly execute a statement until a certain condition is met.

① for statement

The syntax is as follows:

for(表达式1;表达式2;表达式3)
{
    语句序列
}

Expression 1: initialization expression, responsible for completing the initialization of variables.

Expression 2: loop condition expression, the value is a boolean expression, specify the loop condition.

Expression 3: The operation expression after the loop is responsible for modifying variables and changing the loop condition.

        When executing a for loop, first execute expression 1 to complete the initialization of a variable; the next step is to judge the value of expression 2, if the value of expression 2 is true, then enter the loop body; after executing the loop body, press Expression 3 is then evaluated, which is usually an expression that increments or decrements the loop control variable. This cycle is over. The second round of loop starts from calculating expression 2. If expression 2 returns true, the loop continues, otherwise, the entire for statement is skipped.

[Example] Create the Test02 class in the project, and use the for loop in the main method to calculate the sum of all even numbers between 1 and 100.

public class Test02 {
	public static void main(String args[]) {
		int sum=0;
		for(int i=0;i<=100;i+=2) {
			sum=sum+i;
		}
		System.out.println("1~100之间的所有偶数之和为:"+sum);
	}
}

The running result is shown in the figure:

        In programming, sometimes a special syntax format of the for loop is used to achieve an infinite loop. For this infinite loop, you can jump out of the loop through the break statement.

② foreach statement

        The foreach statement is a special simplified version of the for statement and cannot completely replace the for statement, but any foreach statement can be rewritten as a for statement version. foreach is not a keyword, and it is customary to call this special for statement format a foreach statement. The foreach statement provides programmers with great convenience in traversing arrays, etc.

The syntax is as follows:

for(元素变量x;遍历对象obj){//不必对元素变量x进行初始化。
    引用了x的java语句;
}

[Example] Create the Test03 class in the project, define a one-dimensional array in the main method, and use the foreach statement to traverse the array.

public class Test03 {
	public static void main(String args[]) {
		int arr[]= {4,6,2};
		System.out.println("一维数组中的元素分别为:");
		for(int x:arr) {
			//foreach语句,int x引用的变量,arr指定要循环遍历的数组,最后将x输出
			System.out.println(x);
		}
	}
}

The running result is shown in the figure:

2. Loop control statement 

(1) break statement

        Use the break statement to jump out of the switch structure. In the loop structure, the break statement can also be used to jump out of the current loop body, thereby interrupting the current loop.

The form of using the break statement in the three loop statements is as follows:

while(...)
{
    ...
    break;
    ...
}
do
{
    ...
    break;
    ...
}while(...);
for
{
    ...
    break;
    ...
}

[Example] Use break to jump out of the loop

public class Test04 {
	public static void main(String[] args) {
		for(int i=0;i<=100;i++) {
			System.out.println(i);
			if(i==5) {
				break;
			}
		}
		System.out.println("----end----");
	}
}

 The running result is shown in the figure:

 If you want to break out of the outer loop, Java provides the function of "label", the syntax is as follows:

标签名:循环体{
    break标签名;
}

Tag name: any identifier.

Loop body: any loop statement;

break label name: break jumps out of the specified loop body, and the label name of this loop body must be consistent with the label name of break.

A break with a label can specify the loop to jump out of. This loop can be an inner loop or an outer loop.

[Example] Break out of the outer loop with a labeled break.

public class Test05 {
	public static void main(String[] args) {
		Loop:for(int i=0;i<3;i++) {
			for(int j=0;j<6;j++) {
				if(j==4) {
					break Loop;
				}
				System.out.println("i="+i+"j="+j);
			}
		}
	}
}

The running result is shown in the figure:

 (2) continue statement

        The continue statement is a supplement to the break statement. continue does not jump out of the loop body immediately, but jumps out of the statement before the end of the loop, returns to the condition test part of the loop, and restarts the row loop. When continue is encountered in a for loop statement, the increment portion of the loop is executed first, followed by the conditional test. In while and do...while loops, the continue statement returns control directly to the conditional testing section.

Among the three loop statements, the form of using the continue statement is as follows:

while(...)
{
    ...
    continue;
    ...
}
do
{
    ...
    continue;
    ...
}while(...);
for
{
    ...
    continue;
    ...
}

[Example] Output an odd number between 1 and 20, use continue to jump out of the loop.

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

The running result is shown in the figure:

 Like the break statement, continue also supports the label function, and the syntax is as follows:

标签名:循环体{
    continue标签名;
}

Tag name: any identifier.

Loop body: any loop statement.

continue label name: continue jumps out of the specified loop body, and the label name of this loop body must be consistent with the label name of continue.

Guess you like

Origin blog.csdn.net/weixin_65089091/article/details/127818913