Skip to content

Java break Statement | Label with Java break for loop example

  • by

Java break Statement is Used when you want to immediately terminate a Loop and the next statements will execute. It mainly used for loop, switch statement, for each loop or while Lopp, etc.

It’s introduced in Java since JDK 1.5

Java break Statement, Label with Java break for loop , while and if example

A Detailed about Java break statement in the program has the following two usages −

  • An encounter inside a loop to immediately terminated and the program control resumes at the next statement following the loop.
  • Used to terminate a case in the switch statement

In this tutorial, you will learn how to use the Break statement with for loop, while Loop, etc. And you must know about the if condition statement because it will use many examples.

Syntax

Here is simple break statement syntax in java

Loop{
    break;
    // more codes 
}

Flow Diagram of Java break

Here is a flowchart.

Flow Diagram of Java break how it works

Java break for loop Example

Here is the first example of how to work with the break statement in Python. In the example using for Loop and checking a condition with if statement. if i = 4 then break the loop.

public class BreakExampleForLoop {

    public static void main(String[] args) {
        //Using for loop
        for (int i = 0; i <= 10; i++) {
            if (i == 4) {
                //breaking the loop
                System.out.println("Breaking the loop at " +  i);
                break;
            }
            System.out.println(i);
        }
    }
}

Output: 0
1
2
3
Breaking the loop at 4

How to break for loop Works

You see the above example, Here we are showing How actually it’s worked.

How Java break for loop Works statement

Java break label

Using a break statement with the label you can stop any loop in Java whether it is an Outer Loop or Inner Loop. See the Java break label example code below.

public class BreakExampleLabel {

    public static void main(String[] args) {
        label:
        for (int i = 1; i <= 2; i++) {
            System.out.println("Parent " + i);
            for (int j = 1; j <= 3; j++) {
                if (i == 2 && j == 2) {
                    //Using break statement with label
                    break label;
                }
                System.out.println("Child " + j);
            }
        }
    }
}

Output: Parent 1
Child 1
Child 2
Child 3
Parent 2
Child 1

Java break Switch Control 

Hope you did read Java Switch statement tutorial and example, Where we used a break in Switch control statement.

If not do follow the link and check out this example.

public class BreakExamplSwitch {
    public static void main(String[] args) {
        //Declaring a variable 
        int number = 2;
        //Switch expression
        switch (number) {
            //Case statements
            case 1:
                System.out.println("Value 1");
                break;
            case 2:
                System.out.println("Value 2");
                break;
            case 3:
                System.out.println("Value 3");
                break;
                //Default case statement
            default:
                System.out.println("Not in 1, 2 or 3");
        }
    }
}

Output: Value 2

Java Break Statement in while loop

Let’s see last one more example of Break statement in While Loop.

public class BreakExample {

    public static void main(String[] args) {
        //while loop
        int i = 1;
        while (i <= 9) {
            if (i == 3) {
                // break statement
                i++;
                break;//it will break the loop
            }
            System.out.println(i);
            i++;
        }
    }
}

Output: 1
2

If you any doubts or suggestions then do comment in below.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Examples Break Statement with loops and control statement are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *