Home  >  Article  >  Java  >  [java tutorial] Java loop structure - for, while and do...while

[java tutorial] Java loop structure - for, while and do...while

黄舟
黄舟Original
2016-12-26 11:42:241436browse

Java loop structure - for, while and do...while

The program statements of the sequence 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

in Java5 Introduced an enhanced for loop primarily 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 the condition is not met, then Cannot enter the loop. But sometimes we need to execute it at least once even if the conditions are not met.

do…while loop is similar to while loop. The difference is that 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 executed first. One or more loop control variables can be declared and initialized, or it can be an empty statement.

Then, detect 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 variable.

Detect 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

Java5 introduces an enhanced type mainly used for arrays for loop.

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. Jump out of the entire statement block.

break jumps out of the innermost loop and continues to execute the statements below the loop.

语法

break的用法很简单,就是循环结构中的一条语句:

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");
      }
   }
}

以上实例编译运行结果如下:

10
20

continue关键字

continue适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

在for循环中,continue语句使程序立即跳转到更新语句。

在while或者do…while循环中,程序立即跳转到布尔表达式的判断语句。

语法

continue就是循环体中一条简单的语句:

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");
      }
   }

以上实例编译运行结果如下:

10
20
40
50

 以上就是【java教程】Java循环结构 - for, while 及 do...while的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn