工作小驛

          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
          主站蜘蛛池模板: 白水县| 台东县| 尉氏县| 米林县| 余庆县| 蕉岭县| 涡阳县| 留坝县| 左权县| 开封市| 鹰潭市| 安乡县| 上林县| 那坡县| 永修县| 穆棱市| 通化市| 淮安市| 清河县| 思南县| 沂水县| 新巴尔虎左旗| 乐清市| 汤原县| 竹北市| 合川市| 浮梁县| 庄浪县| 丽江市| 阜阳市| 儋州市| 松桃| 东平县| 岑巩县| 温宿县| 曲阳县| 黑山县| 怀柔区| 平原县| 安溪县| 定安县|