package com.kongdechang.javase.note004;
public class XunHuan {
public static void main(String[] args) {
/**
* break 关键字
* break 主要用在循环语句或者 switch 语句中,用来跳出整个语句块。
*
* break 跳出最里层的循环,并且继续执行该循环下面的语句。
*/
int[] num = {1,2,3,4,5,6};
for (int i = 0; i < num.length; i++) {
if (i%3==0) {
System.out.println("循环次数 = " + i + " num " + num[i]);
break; // break 跳出循环。
}
}
int x = 2;
switch (x){
case 1:
System.out.println(1);
break;
case 2:
System.out.println(2);
break;
case 3:
System.out.println(3);
break;
default:
System.out.println("不匹配");
break;
}
System.out.println("----------------------------------------");
/**
* continue 关键字
* continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。
*
* 在 for 循环中,continue 语句使程序立即跳转到更新语句。
*
* 在 while 或者 do…while 循环中,程序立即跳转到布尔表达式的判断语句。
*/
for (int i = 0; i < num.length; i++) {
if(i%2==0){
System.out.println("循环次数 = " + i + " num " + num[i]);
continue;//立刻跳转到下一次循环
}
}
for(int i : num ) {
if (x == 3) {
continue;
}
}
int y=9,p = 0;
while (p<y){
p++;
if (p%y==0){
System.out.println("本次跳过");
continue;
}
}
}
}
break continue 关键字
最新推荐文章于 2023-11-26 17:32:22 发布