注册
 找回密码
 注册
江西广告网
查看: 303|回复: 0
打印 上一主题 下一主题

java初学者实践教程7-跳转语句

[复制链接]

该用户从未签到

1
跳转到指定楼层
发表于 2009-1-5 08:59:43 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?注册

x
上一节我们说有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的拆箱与装箱 查看原文>>
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表