java--跳转语句
循环跳转语句 :
break [label] //用来从语句、循环语句中跳出。
continue [label] //跳过循环体的剩余语句,开始下一次循环。
代码
class BreakLoop {
public static void main(String args[]) {
for(int i=0; i<100; i++) {
if(i = = 10) break; // terminate loop if i is 10
System.out.println("i: " + i);
}
System.out.println("Loop complete.");
}
}
跳出switch
代码
class SampleSwitch {
public static void main(String args[]) {
for(int i=0; i<6; i++)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than 3.");
}
}
}
2、 continue语句
class Continue {
public static void main(String args[]) {
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if (i%2 = = 0) continue;
System.out.println("");
}
}
}