江西广告网

标题: java初学者实践教程7-跳转语句 [打印本页]

作者: 樱子    时间: 2009-1-5 08:59
标题: java初学者实践教程7-跳转语句
上一节我们说有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."); } } 下载 href="http://java.chinaitlab.com/download/07070515349394.rar" target=_blank>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(); } } 下载 href="http://java.chinaitlab.com/download/07070515403787.rar" target=_blank>此例子打包下载 java初学者实践教程8—jdk5的拆箱与装箱 查看原文>>




欢迎光临 江西广告网 (http://bbs.jxadw.com/) Powered by Discuz! X3.2