工作小驛

          Ninja!

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            103 Posts :: 0 Stories :: 36 Comments :: 0 Trackbacks
           昨天我們說(shuō)有4類程序控制語(yǔ)句,但是才講了2個(gè)。今天講跳轉(zhuǎn)語(yǔ)句。異常處理語(yǔ)句我們找一節(jié)專題來(lái)講。

               循環(huán)跳轉(zhuǎn)語(yǔ)句 break [label] //用來(lái)從語(yǔ)句、循環(huán)語(yǔ)句中跳出。

                                             continue [label] //跳過(guò)循環(huán)體的剩余語(yǔ)句,開(kāi)始下一次循環(huán)。

                 這兩個(gè)語(yǔ)句都可以帶標(biāo)簽(label)使用,也可以不帶標(biāo)簽使用。標(biāo)簽是出現(xiàn)在一個(gè)語(yǔ)句之前的標(biāo)識(shí)符,標(biāo)簽后面要跟上一個(gè)冒號(hào)(:),標(biāo)簽的定義如下:

                 label:statement;

          實(shí)踐:

          1、 break語(yǔ)句

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

          }

          }

          }

           

          // 跳出循環(huán)

          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個(gè)break跳出循環(huán)的例子下載

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

          }

          }

          這個(gè)在昨天的分支語(yǔ)句中,我們就已經(jīng)學(xué)到了。

           

          2、 continue語(yǔ)句

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

          }

          }

          }

          //帶標(biāo)簽的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();

          }

          } 此例子打包下載

          posted on 2007-07-22 17:15 王君 閱讀(177) 評(píng)論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 吉水县| 固始县| 峨山| 蓝田县| 宝丰县| 随州市| 绵阳市| 商城县| 日照市| 吉安市| 南平市| 东明县| 湖州市| 壶关县| 榕江县| 隆昌县| 高阳县| 环江| 彰武县| 剑阁县| 乌兰察布市| 漳州市| 隆德县| 黎川县| 石棉县| 多伦县| 汝城县| 行唐县| 延寿县| 郯城县| 阳江市| 斗六市| 门源| 乌兰浩特市| 抚州市| 乐平市| 兰溪市| 深泽县| 皮山县| 黄石市| 威海市|