day03--循环的控制-break关键字

This commit is contained in:
2026-09-17 11:37:48 +08:00
parent 1dbb5c425e
commit d1e15efe98
@@ -0,0 +1,25 @@
package com.inmind.controller05;
/*
循环控制_break
break的作用:
中断循环
中断switch
*/
public class Demo09 {
public static void main(String[] args) {
/*int a = 4;
if (a == 4) {
break;//错误: break只在 switch 或 loop 内部使用
}*/
//跑圈1~10,有个人第4圈开始就不跑了
for (int i = 1; i < 11; i++) {
//如果是第四圈,就不跑(结束,中断)
if (i == 4) {
break;//中断循环
}
System.out.println(i);
}
System.out.println("程序结束");
}
}