Java loop structure
The program statements of the sequential structure can only be executed once. If you want to perform the same operation multiple times, you need to use a loop structure.
There are three main loop structures in Java:
while loop
do...while loop
for loop
Introduced in Java5 an enhanced for loop mainly for arrays.
while loop
While is the most basic loop, its structure is:
while( 布尔表达式 ) { //循环内容 }
As long as the Boolean expression is true, the loop body will continue to execute.
Example
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("\n"); } } }
The compilation and running results of the above example are as follows:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
do...while loop
For the while statement, if it is not satisfied condition, you cannot enter the loop. But sometimes we need to execute it at least once even if the conditions are not met.
The do...while loop is similar to the while loop. The difference is that the do...while loop will be executed at least once.
do { //代码语句 }while(布尔表达式);
Note: The Boolean expression is after the loop body, so the statement block has been executed before detecting the Boolean expression. If the Boolean expression evaluates to true, the statement block is executed until the Boolean expression evaluates to false.
Example
public class Test { public static void main(String args[]){ int x = 10; do{ System.out.print("value of x : " + x ); x++; System.out.print("\n"); }while( x < 20 ); } }
The compilation and running results of the above example are as follows:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
for loop
Although all loop structures can use while or do. ..while means, but Java provides another statement-the for loop, which makes some loop structures simpler.
The number of times the for loop is executed is determined before execution. The syntax format is as follows:
for(初始化; 布尔表达式; 更新) { //代码语句 }
There are several instructions for the for loop:
The initialization step is performed first. A type can be declared, but one or more loop control variables can be initialized, or it can be an empty statement.
Then, test the value of the Boolean expression. If true, the loop body is executed. If it is false, the loop terminates and execution of the statements following the loop body begins.
After executing the loop once, update the loop control variables.
Check the Boolean expression again. Perform the above process in a loop.
Example
public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); } } }
The compilation and running results of the above example are as follows:
value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19
Java enhanced for loop
Introduced in Java5 An enhanced for loop primarily for arrays.
Java enhanced for loop syntax format is as follows:
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: The expression is the name of the array to be accessed, or a method that returns an array.
Example
public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); } System.out.print("\n"); String [] names ={"James", "Larry", "Tom", "Lacy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); } } }
The compilation and running results of the above example are as follows:
10,20,30,40,50, James,Larry,Tom,Lacy,
break keyword
break is mainly used in loop statements or switch statements , used to jump out of the entire statement block.
break jumps out of the innermost loop and continues to execute the statements below the loop.
Grammar
The usage of break is very simple, it is a statement in the loop structure:
break;
Example
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { break; } System.out.print( x ); System.out.print("\n"); } } }
The compilation and running results of the above example are as follows:
10 20
continue keyword
continue applies to any loop control structure. The function is to allow the program to immediately jump to the next iteration of the loop.
In the for loop, the continue statement causes the program to jump immediately to the update statement.
In a while or do...while loop, the program immediately jumps to the judgment statement of the Boolean expression.
Grammar
continue is a simple statement in the loop body:
continue;
Example
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print( x ); System.out.print("\n"); } } }
The compilation and running results of the above example are as follows:
10 20 40 50