The best Java looping constructs - for, while and do ... while Tutorial In 2024, In this tutorial you can learn while loop,do ... while loop,for loop,Java enhanced for loop,break keyword,continue keywords,
Program statements sequential structure can only be performed once. If you want to perform the same operation many times ,, you need to use a loop structure.
Java has three main loop structure:
Introduced an enhanced array is mainly used for the loop in Java5.
while loop is the most basic, its structure is:
while( 布尔表达式 ) { //循环内容 }
As long as the Boolean expression is true, the loop experience has been implemented.
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 above examples compiled results 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 the while statement, if the condition is not satisfied, it can not enter the circulation. But sometimes we need even if conditions are not satisfied, but also to perform at least once.
do /en. while loop and while loop is similar, except that, do /en. while loop will execute at least once.
do { //代码语句 }while(布尔表达式);
Note: The boolean expression after the body of the loop, so that a block of statements prior to detection Boolean expression has been performed. If the Boolean expression evaluates to true, the statement block is executed until the Boolean expression evaluates to false.
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 above examples compiled results 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
While all loop structure can be used while or do /en. while showing, but Java provides another statement - for circulation, so some of the cyclic structure easier.
The number of cycles performed for a determined before executing it. Syntax is as follows:
for(初始化; 布尔表达式; 更新) { //代码语句 }
Respect for loop has the following description:
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 above examples compiled results 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
Java5 for the introduction of a major array of enhanced for loop.
Java enhanced for loop syntax is as follows:
for(声明语句 : 表达式) { //代码句子 }
Disclaimer statement: declare a new local variable, and the type of the variable must match the type of array elements. Its scope is limited to the loop block, its value at this time is equal to the value of the array element.
Expression: Expression is the name of the array to be accessed, or the method that returns the value of the array.
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 above examples compiled results are as follows:
10,20,30,40,50, James,Larry,Tom,Lacy,
break mainly used in loops or switch statements, used to jump out of the whole block.
break out of the innermost loop, and the loop continues to execute the following statement.
break usage is very simple loop structure in a statement:
break;
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 above examples compiled results are as follows:
10 20
continue for any loop control structure. Role is to allow the program to jump immediately to the next iteration of the loop.
In a for loop, continue statement causes the program jumps immediately to update statements.
In while or do /en. while loop, the program jumps immediately to the judge sentences Boolean expressions.
continue the loop body is a simple statement:
continue;
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 above examples compiled results are as follows:
10 20 40 50