HOWTO · Java
Java 中 break 和 continue 語句的區別
本教程演示了 Java 中 break 和 continue 語句之間的區別。
本頁內容
本教程將演示 Java 的 break 和 continue 語句之間的區別。
Java 中的 break 語句用於在給定條件下離開迴圈,而 continue 語句用於在給定條件下跳轉迴圈的迭代。
通過 Java 中的 for 迴圈演示 Break 和 Continue 之間的區別
請參閱下面的示例,以使用 for 迴圈區分 break 和 continue。
public class Break_Continue_Class {
public static void main(String args[]) {
System.out.println("The Break Statement: \n");
for (int x = 0; x < 15; x++) {
if (x == 8) {
break;
}
System.out.println(x);
}
System.out.println("The Continue Statement: \n");
for (int x = 0; x < 15; x++) {
if (x == 8) {
continue;
}
System.out.println(x);
}
}
}
輸出:
The Break Statement:
0
1
2
3
4
5
6
7
The Continue Statement:
0
1
2
3
4
5
6
7
9
10
11
12
13
14
帶有 break 語句的迴圈在 8 處終止,而帶有 continue 的迴圈在 8 處跳轉迭代。對於 while 和 do-while 迴圈的工作方式類似。
通過 Java 中的 foreach 迴圈演示 Break 和 Continue 之間的區別
請參閱下面的示例以區分 foreach 迴圈的 break 和 continue 語句。
public class Break_Continue_Class {
public static void main(String args[]) {
int[] test_array = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
System.out.println("The Break statement works in this way: \n");
for (int test : test_array) {
if (test == 5) {
break;
}
System.out.print(test);
System.out.print("\n");
}
System.out.println("The Continue statement works in this way: \n");
for (int test : test_array) {
if (test == 5) {
continue;
}
System.out.print(test);
System.out.print("\n");
}
}
}
輸出:
The Break statement works in this way:
0
1
2
3
4
The Continue statement works in this way:
0
1
2
3
4
6
7
8
9
如圖所示,帶有 break 語句的迴圈在 5 處終止,帶有 continue 的迴圈在 5 處跳轉迭代。
通過 Java 中的巢狀迴圈演示 Break 和 Continue 之間的區別
下面的示例使用帶有 break 和 continue 語句的巢狀 for 迴圈。
public class Break_Continue_Class {
public static void main(String[] args) {
System.out.println("The Break Statement: \n");
for (int x = 1; x <= 4; x++) {
for (int y = 1; y <= 4; y++) {
if (x == 3 && y == 3) {
break;
}
System.out.println(x + " " + y);
}
}
System.out.println("The Continue Statement: \n");
for (int x = 1; x <= 4; x++) {
for (int y = 1; y <= 4; y++) {
if (x == 3 && y == 3) {
continue;
}
System.out.println(x + " " + y);
}
}
}
}
輸出:
The Break Statement:
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
4 1
4 2
4 3
4 4
The Continue Statement:
1 1
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
4 4
正如我們所見,break 語句僅中斷 x 和 y 均為 3 的內部迴圈,而 continue 語句僅跳過了一次迭代,其中 x 和 y 均為 3。
演示 Java 中帶標籤的 break 和帶標籤的 continue 語句之間的區別
未標記的 break 和 continue 語句僅適用於巢狀迴圈中的最內層迴圈。這些標籤僅用於將宣告應用於我們的選擇。
class Break_Continue_Labeled {
public static void main(String args[]) {
System.out.println("The Labeled Break Statement: ");
first_break: // First label
for (int x = 0; x < 4; x++) {
second_break: // Second label
for (int y = 0; y < 4; y++) {
if (x == 2 && y == 2) {
// Using break statement with label
break first_break;
}
System.out.println(x + " " + y);
}
}
System.out.println("The Labeled Continue Statement: ");
first_continue: // First label
for (int x = 0; x < 4; x++) {
second_continue: // Second label
for (int y = 0; y < 4; y++) {
if (x == 2 && y == 2) {
// Using break statement with label
continue first_continue;
}
System.out.println(x + " " + y);
}
}
}
}
輸出:
The Labeled Break Statement:
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
The Labeled Continue Statement:
0 0
0 1
0 2
0 3
1 0
1 1
1 2
1 3
2 0
2 1
3 0
3 1
3 2
3 3
上面的程式碼使用標籤作為第一和第二。如果我們將 first 作為引數傳遞給語句,它將應用於第一個語句,如果我們傳遞 second,它將應用於第二個語句。
演示 Java 中 switch 條件中 break 和 continue 的使用
只有 break 語句用於 switch 條件; continue 語句沒有用處。
public class Switch_Break_Class {
public static void main(String[] args) {
String[] test_array = {"Demo String", "Delftstack1", "Delftstack2", "Delftstack3"};
for (String test : test_array) {
switch (test) {
case "Delftstack1":
System.out.println(test);
break;
case "Delftstack2":
System.out.println(test);
break;
case "Delftstack3":
System.out.println(test);
break;
default:
System.out.println("Not Delftstack");
}
}
}
}
輸出:
Not Delftstack
Delftstack1
Delftstack2
Delftstack3
上面的程式碼包含三種情況和一種預設情況,它顯示了開關條件中的 break 語句。