蘋果 Apple Watch S3 原型機曝光:配備智慧聯結器,可實現血壓監測
阿新 • • 發佈:2021-06-25
什麼是break
break 跳出最裡層的迴圈,並且繼續執行該迴圈下面的語句 就是強制退出迴圈
示例
package struct; public class ForDemo05 { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; for (int i: numbers) { if (i == 40) { break; } System.out.print(i + "\t"); // 10 20 30 // 40 50沒有輸出 因為break已經跳出了當前迴圈 沒有執行後面的語句 } } }
什麼是continue
讓程式立刻跳轉到下一次迴圈的迭代 跳出當前迴圈 執行下一次
示例
package struct; public class ForDemo06 { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; for (int i: numbers) { if (i == 40) { continue; } System.out.print(i + "\t"); // 10 20 30 50 // 40沒有輸出 因為continue跳過了此次迴圈 } } }