Java-循环控制


循环语句是用来实现多次执行一段代码的。

Java编程语言提供了以下类型的循环来处理循环需求。

循环类型描述
while在给定条件为真时重复一个语句或一组语句。它在执行循环体之前测试条件。
for多次执行一系列语句,并简化循环变量的代码。
do...while类似于while语句,不同之处在于它在循环体的末尾测试条件。

while循环

Java编程语言中的while循环语句只要给定的条件为真,就会重复执行目标语句。

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

输出

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循环

for循环是一种重复控制结构,它允许你编写一个需要执行特定次数的循环。

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

输出

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循环

注意,布尔表达式出现在循环的最后,所以循环中的语句在测试布尔表达式之前执行一次。

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

输出

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支持以下控制语句。

循环控制语句描述
break终止循环或切换语句。
continue使循环跳过后面的部分,并在重新进行迭代之前立即测试其循环条件。

break语句

Java编程语言中的break语句有以下两种用法:

  • 当在循环内遇到break语句时,循环会立即终止,程序在循环后的下一条语句处继续执行。

  • 它可以用来终止switch语句中的case(下一章会讲到)。

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循环中,程序立即跳转到布尔表达式。

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中增强的for循环


从Java 5开始,引入了增强的for循环,这主要用于遍历元素(包括数组)的集合。

语法

for(declaration : expression) {
   // Statements
}
  • 声明 - 新声明的块变量的类型与你要访问的数组的元素兼容,该变量将在for块中可用,并且其值将与当前数组元素相同。

  • 表达式 - 这将求值为你需要遍历的数组,该表达式可以是数组变量或返回数组的方法调用。

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

这将产生以下结果:

10, 20, 30, 40, 50,
James, Larry, Tom, Lacy,

接下来是什么?


在下一章中,我们将学习Java编程中的决策语句。