JavaRush /Java Blog /Random-TW /Java 中的 Break 語句
iloveski
等級 37
Москва

Java 中的 Break 語句

在 Random-TW 群組發布
向所有同事和那些只是有興趣的人問好。
Java 中的 Break 語句 - 1
今天讓我們來看看Java程式語言的一個特性,也就是break運算子。這是關於轉換運算符主題的第一篇文章,所以如果您喜歡它,我很樂意寫更多文章。我想立即指出,本文主要針對初學者開發人員,更高級的學生可能不會在這裡找到任何新內容。那麼,我們走吧。在Java中,有2種方法可以改變程式指令的執行順序:跳躍語句和異常處理機制。java中有3個跳轉運算元:break、continue、return。今天我邀請您仔細了解一下運營商break。異常處理是一個非常廣泛的主題,超出了本文的範圍。java中break語句的使用方法有3種。第一種也是最受歡迎的方法是使用它break提前退出循環,例如:
public class SimpleExample {
    public static void main(String[] args) {
        findNumberInLoop(3);
    }
    public static void findNumberInLoop(int number){
        for (int i = 0; i < 10; i++) {
            if (i == number) {
                break;
            }
            System.out.println(i);
        }
        System.out.println("cycle was finished");
    }
}
此範例將顯示:
1
2
cycle was finished
這意味著當循環計數器i等於傳遞的參數number,即數字3時,循環執行提前終止。如果程式有多個巢狀循環,則僅從最內層循環中斷輸出,例如:
public class SimpleExample {
    public static void main(String[] args) {
        findNumberInLoop(3);
    }
    public static void findNumberInLoop(int number){
        for(int i = 0; i < 5; i++) {
            for (int j =0; j < 10; j++) {
                if(j == number) {
                    break;
                }
                System.out.print(j);
            }
            System.out.println();
        }
        System.out.println("cycles was finished");
    }
}
此範例產生輸出:
012
012
012
012
012
cycles was finished
從範例中可以看出,內循環每次在數字 3(參數number)中斷,外循環執行了 5 次,符合預期。在java中,你可以在一個迴圈中使用多個break語句,但不建議這樣做,因為程式碼的可讀性和結構會大大降低。第二種用途break是用它來中斷branch中語句的執行switch,例如:
public class SimpleExample {
    public static void main(String[] args) {
        square(2);
    }

    public static void square(int number){
        switch (number){
            case 1:
                System.out.println(number*number);
                break;
            case 2:
                System.out.println(number*number);
                break;
            case 3:
                System.out.println(number*number);
                break;
        }
        System.out.println("after switch");
    }
}
運行此範例將產生輸出:
4
after switch
Break 語句將控制權傳遞給整個區塊後面的程式碼switchbreak如果在switch此範例中 未指定:
public class SimpleExample {
    public static void main(String[] args) {
        square(2);
    }
    public static void square(int number){
        switch (number){
            case 1:
                System.out.println(number*number);
            case 2:
                System.out.println(number*number);
            case 3:
                System.out.println(number*number);
        }
        System.out.println("after switch");
    }
}
輸出將不會如預期:
4
4
after switch
發生這種情況是因為在執行該區塊之後,case 2程式繼續執行所有後續的case區塊,這給了我們相應的輸出。有時候算子的這個特性switch 對我們來說是有好處的,例如:
public class SimpleExample {
    public static void main(String[] args) {
        compare(2);
        compare(7);
        compare(12);
    }
    public static void compare(int number){
        switch (number){
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                System.out.println("number is less than 5 or equal 5");
                break;
            case 6:
            case 7:
            case 8:
            case 9:
                System.out.println("number is greater than 5");
                break;
            default:
                System.out.println("number is less than 0 or greater than 9");
        }
        System.out.println("after switch");
    }
}
程式將給定數字與數字 5 進行比較,並給出以下輸出:
after switch
number is greater than 5
after switch
number is less than 0 or greater than 9
after switch
當第一次呼叫某個方法時,操作符switch將控制權轉移給該操作符,然後再轉移給中的case 2操作符。第二個呼叫與第一個類似,但以 c 開頭。第三次呼叫在所有情況下都沒有找到合適的值,因此執行該運算子。第三種方法是使用運算子來取代C 運算子。為此,我們將使用標籤的特殊形式的運算子。這種形式看起來像一個標籤 - 這通常是來自 java 命名空間的任何合適的名稱,位於程式碼區塊之前。而且,給這塊程式碼加上標籤後,程式碼會從對應標籤的程式碼區塊後面繼續執行,例如: breakcase 5case 7defaultbreakgotobreakbreakbreak метка;break
public class SimpleExample {
    public static void main(String[] args) {
        howWeUseLabels(true);
        System.out.println();
        howWeUseLabels(false);
    }
    public static void howWeUseLabels(boolean bool) {
        firstBlock:{
            secondBlock:{
                thirdBlock:{
                    System.out.println("We will see it always");
                    if(bool) {
                        break secondBlock;
                    }
                    System.out.println("We won't see it if bool == true");
                }
                System.out.println("We won't see it if bool == true");
            }
            System.out.println("We will see it always");
        }
    }
}
此範例將產生以下輸出:
We will see it always
We will see it always

We will see it always
We won't see it if bool == true
We won't see it if bool == true
We will see it always
在第一個方法呼叫之後,運算子break將控制權轉移到區塊的末尾secondBlock,退出secondBlock和,同時在末尾thirdBlock執行另一個運算符。第二次通話後,所有線路都會顯示在螢幕上。標籤也可用於指示循環。應該記住,您只能從標籤指示的程式碼區塊內部轉到標籤;例如,這樣的程式碼將無法編譯: println()firstBlock
public class SimpleExample {
    public static void main(String[] args) {
        label:{
            System.out.println("inside label");
        }
        if(true) {
            break label;
        }
        System.out.println("This is error");
    }
}
關於程式碼和語句清潔度的註解break:普遍接受的建議是非常小心地使用語句break,因為這種使用break會使循環更難以理解,並迫使程式設計師查看循環內部以了解其操作的細微差別。有一條啟發式規則,break 您可以在較短的循環中更加自信地使用它,而在長而深的嵌套循環中則可以更加謹慎地使用,因為這會增加出現錯誤的可能性並使其他程序員閱讀和理解您的程式碼變得複雜。正如我上面所寫的,在程式碼中使用大量的break. 表明程式設計師沒有完全理解為什麼他要這樣編寫程式碼。可能值得考慮將其分解為幾個較小的循環來重構這樣的循環。break 使用標籤代替時要小心goto,因為這會使程式碼結構變得複雜。這就是我今天想告訴你的關於breakjava中的運算符的全部內容。我很樂意接受建設性的批評,如果您有興趣的話, 我也準備寫一篇關於運算符continue和 的續篇。return
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION