Java 简明教程

Java - break Statement

Java break Statement

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

The break statement in Java programming language has following two usages −

  1. When the break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

  2. It can be used to terminate a case in the switch statement (covered in the next chapter).

Syntax

break 的语法是任何循环或 switch 用例中的单语句:

The syntax of a break is a single statement inside any loop or switch case −

break;

Flow Diagram

java break statement

Examples

Example 1: Using break with while loop

在此示例中,我们展示了使用 break 语句来打破 while loop 以打印从 10 到 14 的数字,否则它将会打印到第 19 个元素。这里使用值 10 初始化了 int variable x。然后在 while 循环中,我们检查 x 是否小于 20,并在 while 循环中,我们正在打印 x 的值并使 x 的值加 1。While 循环将运行直到 x 变为 15。一旦 x 为 15,break 语句将会打破 while 循环,程序退出。

In this example, we’re showing the use of a break statement to break a while loop to print numbers starting from 10 to 14 which will otherwise print element till 19. Here we’ve initialized an int variable x with a value of 10. Then in while loop, we’re checking x as less than 20 and within while loop, we’re printing the value of x and incrementing the value of x by 1. While loop will run until x becomes 15. Once x is 15, break statement will break the while loop and program exits.

public class Test {

   public static void main(String args[]) {
      int x = 10;

      while( x < 20 ) {
         if(x == 15){
            break;
         }
         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

Example 2: Using break with for loop

在此示例中,我们展示了在 for loop 中使用 break 语句以打印数组的部分元素而不是所有元素。在这里,我们创建一个整数数组作为数字,并对其初始化一些值。我们创建了一个名为 index 的变量来表示循环中的数组索引,并针对数组的大小检查它,然后将其增加 1。在循环主体中,我们使用索引符号打印数组的元素。一旦遇到 30 作为值,break 语句就会中断 for 循环的流程,程序退出。

In this example, we’re showing the use of a break statement within a for loop to print few elements of an array instead of all elements. Here we’re creating an array of integers as numbers and initialized it some values. We’ve created a variable named index to represent index of the array within for loop, check it against size of the array and incremented it by 1. Within for loop body, we’re printing element of the array using index notation. Once 30 is encountered as value, break statement breaks the flow of for loop and program quits.

public class Test {

   public static void main(String args[]) {
      int [] numbers = {10, 20, 30, 40, 50};

      for(int index = 0; index < numbers.length; index++) {
         if(numbers[index] == 30){
            break;
         }
         System.out.print("value of item : " + numbers[index] );
         System.out.print("\n");
      }
   }
}
value of item : 10
value of item : 20

Example 3: Using break with an infinite loop

在示例中,我们展示使用 break 语句使用 while 循环来中断无限循环。它将继续打印数字,直到 x 的值变为 15.

In this example, we’re showing the use of break statement to break an infinite loop using while loop. It will keep printing the numbers until the value of x becomes 15.

public class Test {

   public static void main(String args[]) {
      int x = 10;

      while( true ) {
         System.out.print("value of x : " + x );
         x++;
         if(x == 15) {
            break;
         }
         System.out.print("\n");
      }
   }
}
value of item : 10
value of item : 11
value of item : 12
value of item : 13
value of item : 14