Latest web development tutorials

Java looping constructs - for, while and do ... while

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:

  • while loop
  • do ... while loop
  • for loop

Introduced an enhanced array is mainly used for the loop in Java5.


while loop

while loop is the most basic, its structure is:

while( 布尔表达式 ) {
	//循环内容
}

As long as the Boolean expression is true, the loop experience has been implemented.

Examples

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

do ... while loop

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 ... while loop and while loop is similar, except that, do ... 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.

Examples

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

for loop

While all loop structure can be used while or do ... 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:

  • The first to perform the initialization procedure. You can declare a type, but you can initialize one or more loop control variables can also be empty statement.
  • Then, the detected value of the Boolean expression. If true, the loop body is executed. If false, the loop terminates, the beginning of the implementation of the statement following the loop.
  • After executing a loop, the loop control variable update.
  • Boolean expression detected again. Loop executes the above procedure.

Examples

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

Java enhanced for loop

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.

Examples

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 keyword

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.

grammar

break usage is very simple loop structure in a statement:

break;

Examples

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 keywords

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 ... while loop, the program jumps immediately to the judge sentences Boolean expressions.

grammar

continue the loop body is a simple statement:

continue;

Examples

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