工作小驛

          Ninja!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            103 Posts :: 0 Stories :: 36 Comments :: 0 Trackbacks
           昨天我們說有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();

          }

          } 此例子打包下載

          posted on 2007-07-22 17:15 王君 閱讀(178) 評論(0)  編輯  收藏 所屬分類: J2SE
          主站蜘蛛池模板: 长子县| 咸宁市| 天祝| 天长市| 微博| 依兰县| 青河县| 白玉县| 共和县| 思茅市| 航空| 海林市| 昌邑市| 厦门市| 连云港市| 通城县| 曲阜市| 金华市| 宜昌市| 莱阳市| 安达市| 汉中市| 宣城市| 探索| 甘洛县| 广灵县| 资中县| 沁水县| 许昌县| 新源县| 伊川县| 洛隆县| 定襄县| 博爱县| 阳西县| 本溪市| 资溪县| 开阳县| 新平| 卓资县| 松江区|