The Break Statement Tutorial

The break statement can only be used inside of the following control flow statements: switch, for, while, and do-while. In my Switch Statement Tutorial, we learned that break is used to terminate fall-through on case statements. There are two types of break statements, unlabeled and labeled. The unlabeled break statement terminates loop iterations for the looping block it is located inside of.
for (initialization; termination; increment) {
      Statements are executed as long as the termination expression is equal to true;
      if (condition) {
            break;
      }
}
// after unlabeled break, code resumes here

What can we do if we have nested loops and we want a conditional test inside of an inner loop to terminate the outer loop as well? We can use the labeled break statement. The first step for using a labeled break statement is to make a label followed by a colon. The label must be located directly before the block of code that it is labeling. When the break statement is called it must have the label name after the keyword break, but before the semicolon. Code execution will resume directly after the end of the code block.
label:
for (initialization; termination; increment) {
      Statements are executed as long as the termination expression is equal to true;
      for (initialization; termination; increment) {
            Statements are executed as long as the termination expression is equal to true;
            if (condition) {
                  break label;
            }
      }
}
// after labeled break, code resumes here



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md BreakStatement
C:\Java>cd BreakStatement
C:\Java\BreakStatement>Notepad BreakStatement.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class BreakStatement {
    public static void main(String args[]) {
        String stringArray[] = {"Duck", "Duck", "Duck", "Duck", "Goose", "Duck", "Duck", "Duck", "Duck" };

        for(int i = 0; i < stringArray.length; i++) {
            System.out.print(stringArray[i] +" ");
            if (stringArray[i].equals("Goose")) {
                System.out.println();
                break;
            }
        }
        System.out.println("Game over");
        System.out.println();
      
        // build a number ladder
        int maxNumber = 5; 
       
        myLabel:
        for (int x=1; true; x++){
            for (int y=1; y<=x; y++) {
                if (x > maxNumber) {
                    break myLabel;
                }
                System.out.print(y + " ");
            }
            System.out.println();
        }
        System.out.println("Program is over");
    }
}


Now switch back to the command prompt (CMD) and type in javac BreakStatement.java and press Enter.
Now type in java BreakStatement and press Enter.


C:\Java\BreakStatement>javac BreakStatement.java
C:\Java\BreakStatement>java BreakStatement
Duck Duck Duck Duck Goose
Game Over

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Program is over


Final thoughts

Finding a good use for the labeled break statement is rare.


Tutorials