工作小驛

          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
          主站蜘蛛池模板: 宣汉县| 阜康市| 南安市| 股票| 恩平市| 锡林郭勒盟| 公主岭市| 新丰县| 石台县| 新密市| 阳信县| 蓝山县| 克什克腾旗| 达孜县| 织金县| 凭祥市| 和平县| 平度市| 塔河县| 天津市| 拉萨市| 西林县| 静安区| 普兰县| 成武县| 德令哈市| 固阳县| 云林县| 揭西县| 讷河市| 望谟县| 丰原市| 古丈县| 宜君县| 大名县| 长宁区| 绥化市| 宁城县| 义乌市| 昌图县| 云林县|