1.控制跳转语句break
*break的使用场景
*在选择结构switch语句中
*防止switch穿透
*在循环语句中
*跳出单层循环
package com.text;
/*
* break:中断的意思
* 使用场景:
* A:switch语句中
* B:循环中
* 注意:
* 离开使用场景是没有意义的。
* 作用:
* 跳出循环,让循环提前结束
*/
public class BreakDemo {
public static void main(String[] args) {
//break;
for(int x=1; x<=10; x++) {
if(x == 3) {
break;
}
System.out.println("HelloWorld");
}
}
}
2.控制跳转语句continue
*在循环中使用,脱离使用场景没有意义
*break 退出单层循环
*continue 退出本次循环
*案例
package com.text;
/*
* continue:继续的意思
* 使用场景:
* 循环中
* 注意:
* 离开使用场景是没有意义的
* 作用:
* 结束一次循环,继续下一次的循环
* 区别:
* break:退出循环
* continue:结束一次循环,继续下一次的循环
*/
public class ContinueDemo {
public static void main(String[] args) {
//continue;
for(int x=1; x<=10; x++) {
if(x == 3) {
//break;
continue;
}
System.out.println("HelloWorld");
}
}
}