应用场景
- 当break语句出现在循环内时,程序会立刻跳出循环体,继续执行循环体后面的代码语句。
- 当break用于switch语句时,程序会结束当前的case继续执行后面的语句(后面章节中会介绍)。
语法
在循环中使用break时,只需要写上break关键字然后跟上分号即可:
break;
流程图
举例
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
关注公众号「小白轻松学编程」
更多交流,欢迎微信搜索并关注公众号「小白轻松学编程」!
博客里所有教程会第一时间在公众号上更新哟,扫码关注一下吧~