Break与continue
Break与continue
//break退出整个循环
//continue跳过本次尚未执行的操作
public class BreakDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
System.out.println(i);
if (i==30){
break;
}
}
System.out.println("123");
}
}
public class ContinueDemo {
public static void main(String[] args) {
int i = 0;
while (i<100){
i++;
if (i%10==0){
System.out.println();
continue;
}
System.out.print(i);
}
//break退出整个循环
//continue跳过本次尚未执行的操作
}
}
ps:带标签的continue,类似于goto
public class LabelDemo {
public static void main(String[] args) {
//打印101-150之间所有的质数
//质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数
int count = 0;
// 类似goto,带标签的continue
outer:for (int i = 101; i < 150; i++) {
for (int j = 2;j<i/2;j++){
if (i % j ==0){
continue outer;
}
}
System.out.print(i+" ");
}
}
}