java break語句的三種用法


1、用於switch語句當中,用於終止語句

2、用於跳出循環,此為不帶標簽的break語句,相當與goto的作用

e.g

 1         while(i<j&&h<k){
 2             if(h<k)
 3             {
 4                 ....
 5             }
 6         }
 7         
 8         while(i<j){
 9             if(h>k)  break;
10         }

在第一種寫法中,對條件h<k進行了兩次檢測,而第二種寫法,通過使用break跳出循環,減少了對同一條件的判別次數,避免了重復檢測。

注意,在一系列循環嵌套中,break僅僅終止的是最里層的循環

試驗代碼如下:

 1 for(int i=0;i<=3;i++){
 2             System.out.print("loop"+i+" ");
 3             for(int j=0;j<10;j++){
 4             if(j==3){
 5                 break;
 6             }
 7             System.out.print("j="+j+" ");
 8             }
 9             System.out.println();
10             
11         }

輸出:

loop0 j=0 j=1 j=2
loop1 j=0 j=1 j=2
loop2 j=0 j=1 j=2
loop3 j=0 j=1 j=2

3、帶標簽的break語句

常常用於跳出多層嵌套

注意,在帶標簽的break語句中,標簽必須放在希望跳出的最外層之前,並緊跟一個冒號

e.g

 1 public class Test {
 2     public static void main(String args[]){
 3         
 4         read_data:
 5             while(1==1){
 6                 if(1==1){
 7                     System.out.println("1st");
 8                         
 9                 }
10                 if(1==1){
11                     System.out.println("2nd");
12                     break read_data;
13                     
14                 }
15                 if(1==1){
16                     System.out.println("3rd");
17                 }
18             }
19         
20         System.out.println("out of loop");
21         
22         
23     }
24 }

輸出:

1st
2nd
out of loop

e.g:

 1         first:{
 2         System.out.println("first");
 3         second:{
 4             System.out.println("second");
 5             if(1==1){
 6                 break first;
 7             }
 8             third:{
 9                 System.out.println("third");
10             }
11         }
12         System.out.println("will not be excuted");
13     }
14     System.out.println("out of loop");

輸出:

first
second
out of loop


對於帶標簽的break語句而言,只能跳轉到包含break語句的那個塊的標簽上

下列代碼是非法的:

1         first:if(1==1){
2             System.out.print("................");
3         }
4         second:if(1==1){
5             System.out.print("................");
6             break first;
7         }    

補充 continue語句:

continue語句將控制轉移到最內層循環的首部

while和 do while語句中continue語句直接轉移到條件表達式,在for循環中,循環的檢驗條件被求值。

e.g:

 1 public static void main(String args[]){
 2         
 3         int count;
 4         int n;
 5         int sum=0;
 6         Scanner i=new Scanner(System.in);
 7         for(count=1;count<=3;count++){
 8             System.out.println("enter a number,-1 to quit:");
 9             n=i.nextInt();
10             if(n<0) 
11                 {            
12                 System.out.println("sum="+sum);
13                 continue;
14                 }
15             sum+=n;
16             System.out.println("sum="+sum);
17         }
18 
19     }

輸出:

enter a number,-1 to quit:
1
sum=1
enter a number,-1 to quit:
-1
sum=1
enter a number,-1 to quit:
1
sum=2
當輸入為負時,continue語句直接跳到count++語句

continue語句也可以使用標簽:

e.g

 1 public static void main(String args[]) {
 2 
 3         outer: for (int i=0; i<10; i++) {
 4 
 5         for(int j=0; j<10; j++) {
 6 
 7         if(j > i) {
 8 
 9         System.out.println();
10 
11         continue outer; }
12 
13         System.out.print(" " + (i * j)); }}
14 
15         System.out.println();
16 
17         } 

輸出:

 0
 0 1
 0 2 4
 0 3 6 9
 0 4 8 12 16
 0 5 10 15 20 25
 0 6 12 18 24 30 36
 0 7 14 21 28 35 42 49
 0 8 16 24 32 40 48 56 64
 0 9 18 27 36 45 54 63 72 81

return語句:

return語句將程序控制返回到調用它的方法

e.g:

 1 public static void main(String args[]) {
 2         
 3         for(int i=0;i<10;i++){
 4         System.out.println("i="+i+" ");
 5             for(int j=0;j<10;j++)
 6             {
 7                 if(1==1){
 8                 return;
 9                 }
10                 System.out.println("j="+j+" ");
11             }
12         }
13         }

輸出:

i=0

return 用來是正在執行的分支程序返回到它的調用方法上,在此,main函數的調用方法是java運行系統,所以執行到return;處,程序控制將被傳遞到它的調用者,所以后面的代碼將都不被執行。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM