工作小驛

          Ninja!

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

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

                                             continue [label] //跳過循環(huán)體的剩余語句,開始下一次循環(huán)。

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

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

          }

          }

          }

           

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

          }

          } 5break跳出循環(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.");

          }

          }

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

           

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

          }

          } 此例子打包下載

          posted on 2007-07-22 17:15 王君 閱讀(177) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 琼海市| 大田县| 芜湖市| 池州市| 湟源县| 兴仁县| 甘孜| 大渡口区| 罗定市| 营口市| 台北市| 沧源| 沅陵县| 房产| 仲巴县| 东辽县| 额济纳旗| 清远市| 垣曲县| 青阳县| 自治县| 托里县| 上杭县| 湖北省| 驻马店市| 新兴县| 自贡市| 札达县| 阿克苏市| 黄大仙区| 磴口县| 政和县| 资溪县| 汤原县| 合肥市| 和田县| 梁河县| 长兴县| 哈尔滨市| 泾阳县| 神农架林区|