class Break {
public static void main(String
args[]) {
boolean t =
true;
first: {
second: {
third: {
System.out.println("Before the
break.");
if(t) break second; // break out
of second block
System.out.println("This won't
execute");
}
System.out.println("This won't
execute");
}
System.out.println("This is after
second block.");
}
}
}
// 跳出循环
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.");
}
} 5个break跳出循环的例子下载
//跳出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("");
}
}
}
//带标签的continue
class ContinueLabel
{
public static void main(String
args[]) {
outer: for (int i=0; i<10; i++)
{
for(int j=0; j<10; j++)
{
if(j > i) {
System.out.println();
continue outer;
}
System.out.print(" " + (i *
j));
}
}
System.out.println();
}
} 此例子打包下载
|