java之break解析

來源:互聯網
上載者:User

標籤:

在java中,break語句有3種作用。

第一,在switch語句中,它被用來終止一個語句序列。

第二,它能被用來退出一個迴圈。(一系列的迴圈嵌套時,只跳出做裡面的一層)

第三,它能作為一種“先進”的goto 語句來使用。

下面對最後 2種用法進行解釋。 

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

結果:

i: 0 
i: 1 
i: 2 
i: 3 
i: 4 
i: 5 
i: 6 
i: 7 
i: 8 
i: 9 
Loop complete.

class BreakLoop3 { 

public static void main(String args[]) { 

for(int i=0; i<3; i++) { 
System.out.print("Pass " + i + ": "); 
for(int j=0; j<100; j++) { 
if(j == 10) break; // terminate loop if j is 10 
System.out.print(j + " "); 
} 
System.out.println(); 
} 
System.out.println("Loops complete."); 
} 
} 

結果:

Pass 0: 0 1 2 3 4 5 6 7 8 9 
Pass 1: 0 1 2 3 4 5 6 7 8 9 
Pass 2: 0 1 2 3 4 5 6 7 8 9 
Loops complete.  

從中可以看出,在內部迴圈中的break語句僅僅終止了該迴圈,外部的迴圈不受影響。

關於break ,在這裡要記住兩點。

首先,一個迴圈中可以有一個以上的break 語句。

其次,switch語句中的break僅僅影響該switch 語句,而不會影響其中的任何迴圈。

標籤break語句的通用格式如下所示:

break label;

標籤label是標識代碼塊的標籤。當這種形式的break執行時,控制被傳遞出指定的代碼快。被加標籤的代碼必須包圍break語句。

要指定一個代碼塊,在其開頭加一個標籤即可。標籤(label )可以是任何合法有效Java 標識符後跟一個冒號。一旦你給一個塊加上標籤後,你就可以使用這個標籤作為break 語句的對象了。這樣做會使執行在加標籤的塊的結尾重新開始class Break { public static void main(String

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 blockSystem.out.println("This won‘t execute");

        } //third

  System.out.println("This won‘t execute");

      }//second

    System.out.println("This is after second block.");

    } //first
}
}

 

結果:

Before the break.
This is after second block.  

標籤break 語句的一個最普遍的用法是退出迴圈嵌套。例如,下面的程式中,外層的迴圈只執行了一次:

class BreakLoop4 {
public static void main(String args[]) {
outer: for(int i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
for(int j=0; j<100; j++) {
if(j == 10) break outer; // exit both loops
System.out.print(j + " ");
}
System.out.println("This will not print");
}
System.out.println("Loops complete.");
}
}

結果:

 Pass 0: 0 1 2 3 4 5 6 7 8 9 Loops complete.

記住如果一個標籤不在包圍break的塊中定義,你就不能break 到該標籤。例如,下面的程式就是非法的,且不會被編譯:

class BreakErr {
public static void main(String args[]) {
one: for(int i=0; i<3; i++) {
System.out.print("Pass " + i + ": ");
}
for(int j=0; j<100; j++) {
if(j == 10) break one; // WRONG
System.out.print(j + " ");
}
}
} 

因為標籤為one的迴圈沒有包圍break 語句,所以不能將控制傳遞到該塊。

 

java之break解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.