一.break(中止循环)
1.break常用法
class break的常用法{
public static void main(String[] args){
for(int i = 0; i <= 3; i++) {
for (int j = 0; j <= 5; j++) {
if (j == 3) {//输出几次i
break;//终止内层循环,即最近的循环
}
System.out.println("i = " + i + " j = " + j);//j只能输出0/1/2
}
}
}
}
2.break细节
class break细节{
public static void main(String[] args){
label1:
for(int i = 0; i <= 3; i++) {
label2:
for (int j = 0; j <= 5; j++) {
if (j == 3) {//输出几次i
break label1;//终止标签为label1的循环,即终止指定循环
}
System.out.println("i = " + i + " j = " + j);//j只能输出0/1/2
}
}
}
}
二.continue(跳出循环,重新循环)
1.continue常用法
class continue的常用法{
public static void main(String[] args){
for(int i = 0; i <= 3; i++) {
for (int j = 0; j <= 3; j++) {
if (j == 2) {
continue;//跳出内层循环,返回循环
}
System.out.println("i = " + i + " j = " + j);//输出中j无法等于2
}
}
}
}
2.continue细节
class continue的细节{
public static void main(String[] args){
laber1:
for(int i = 0; i <= 3; i++) {
laber2:
for (int j = 0; j <= 3; j++) {
if (j == 2) {
continue laber1;//跳出laber1循环,重新循环
}
System.out.println("i = " + i + " j = " + j);//输出中j无法等于2
}
}
}
}
三.return(终止程序)
1.return常用法
class return的常用法{
public static void main(String[] args){
for(int i = 0; i <= 3; i++) {
for (int j = 0; j <= 5; j++) {
if (j == 3) {
return;//退出程序
}
System.out.println("i = " + i + " j = " + j);//j只能输出0/1/2
}
}
}
}