Break和Continue
Break和Continue
break
- 在循环中,直接打破退出循环
- 在switch中,选择执行,防止穿透
//输出了1-9
public static void main(String[] args) {
int i = 0;
while (i < 50) {
i++;
if (i % 10 == 0) {
System.out.println();
break;
}
System.out.print(i);
}
}
continue
在循环中,跳过该次循环后,继续循环
//输出了1-49,逢10倍数不要
public static void main(String[] args) {
int i = 0;
while (i < 50) {
i++;
if (i % 10 == 0) {
System.out.println();
continue;
}
System.out.print(i);
}
}