您的位置: 文章阅读 ┆ 回首页
 
站内资源搜索:
┆ 将本文寄给朋友  
全新java初学者实践教程7(Java SE5.0版)
基本语法4

作者:100jq

 

文章来自:百家拳软件技术研究室
  
   昨天我们说有
4类程序控制语句,但是才讲了2个。今天讲跳转语句。异常处理语句我们找一节专题来讲。

     循环跳转语句 break [label]  //用来从语句、循环语句中跳出。

                
      
continue [label]  //
跳过循环体的剩余语句,开始下一次循环。

       这两个语句都可以带标签(label)使用,也可以不带标签使用。标签是出现在一个语句之前的标识符,标签后面要跟上一个冒号(:),标签的定义如下:

       label:statement;
                                                                                

实践:

1、  break语句

 

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.");

}

} 5break跳出循环的例子下载

//跳出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();

}

} 此例子打包下载

来源



以上文章版权由原作者所有。未经同意,不得将其任何一部分复制、转载、发布等未授权操作。


Java学习室
E-mail: zz3zcwb@sina.com
COPY RIGHT 2005