Examples of loop structures


Preface

There are three loop structures in Java, namely while, do...while, and for statements.

1. while loop


The most basic loop controls the loop based on whether the loop condition is true or not. If the expression is not true, the loop body will not be executed even once.
Basic syntax:

 while(布尔表达式){
   //循环的语句
   }

Control process:
Insert image description here

2. do…while loop


 do...while循环,与while不同它是先执行循环体再进行判断循环是否要继续下去

Basic syntax:

 do{
 //循环语句
 }while(布尔表达式);

Control process:
Insert image description here

3. for loop


Compared with the previous two loop structures, the for loop is used more frequently and is more complex in redevelopment.
Basic syntax:

for(初始化语句;循环条件;迭代部分表达式){
//循环语句
}

Here are two examples of for loop usage
to print the multiplication table:

//打印乘法表
        for (int i=1;i<=9;i++){  //设置总共输出9行
            for (int j=1;j<=i;j++){ //判断本行要输出几个数据
                System.out.print(i+"*"+j+"="+j*i+"\t"); //输出一个相乘的式子和一个空格
            }
            System.out.println(); //换行 
        }

Print triangles:

//打印三角形
        for (int i= 1;i<=5;i++){
            for(int j=5;j>=i;j--){
                System.out.print(" ");  //输出空格
            }
            for (int j=1;j<=i;j++){
                System.out.print("*"); //用*组成三角形的左半边
            }
            for (int j=1;j<i;j++){
                System.out.print("*");//用*组成三角形的右半边
            }
            System.out.println();
        }

4. Jump out of sentences


If we want to jump out of the loop in advance when executing the loop, we need to mention the usage of several three keywords. There are three ways to jump out of the loop, namely break, continue and return. The meaning and usage of the three keywords:

break ; The .break statement will cause the ongoing program to immediately exit the innermost loop or exit a switch statement. As shown in the following example, after break is executed, subsequent output statements will no longer be executed.

for(int i=0; i < 10; i++){
if(i >= 5){
break;
}
System.out.println("执行");}

cotinue : The continue statement is similar to the break statement. The difference is that it does not exit a loop, but starts a new iteration of the loop. The output statement after cotinue will be executed before jumping out of the current loop.

for(int i=0; i < 10; i++){
if(i == 3 || i == 8){
continue;
}
System.out.println("执行");}}

return : This is the keyword to exit the function of the exit method. The return statement can only be inside a function body, and will cause a syntax error if it appears anywhere else in the code!

The return statement means that a value needs to be returned. If it is not needed, there is no need to use the return statement. They are similar to an exit. Return can end the execution of the code following return in the method body. return false or return true are usually used to determine some results.

Summarize

There are three loop structures in Java, namely while, do...while, and for statements. The while loop is to judge the condition first before looping, and do...while is to execute the loop first and then judge the condition. To break out of the loop, break jumps out of the current loop and no longer executes the remaining loop statements. Cotinue ends the loop and executes the remaining loop statements before jumping out of the loop. Return uses the return value to jump out of the loop.

Guess you like

Origin blog.csdn.net/a545__27/article/details/130009333