编程中break的用法是什么
在Java中,或者在编程中(其实C语言和其它语言也有大致雷同用法),break有三种用法,以下是由学习啦小编整理关于编程中break的用法的内容,希望大家喜欢!
编程中break的用法
学习啦在线学习网 其中的两种非常常用,就是switch语言的break终止和循环语句中(for,While,do While)的break跳出循环。第三种不常用,就是做为“goto“语句跳到指定位置。
先来说说常用的两种:
学习啦在线学习网 1、Switch中的break,跳出语句,代码如下:
public static void main(String[] args) {
int i = 5;
学习啦在线学习网 switch(i){
case 1:
System.out.println("this is "+1);
break;//break根據業務需求來決定是否添加
case 2:
System.out.println("This is "+ 2);
break;
case 3:
System.out.println("This is "+ 3);
break;
case 4:
学习啦在线学习网 System.out.println("This is "+ 4);
break;
case 5:
System.out.println("This is "+ 5);
break;
学习啦在线学习网 default:
学习啦在线学习网 System.out.println("This is ");
}
}
2、break在循环中的使用,这里以for循环为例;
注意:单循环代码如下:
学习啦在线学习网 public static void main(String[] args) {
int i = 10;
for(;i<=100;i++){
if(i==12){
break;
}
学习啦在线学习网 System.out.println("this is i:"+i);
}
}
编程中break的用法是什么




