Java break statement Tutorial

In this section we will learn what the break statement is and how to use it in Java.

Note: we’re assuming you already familiar with either of these loops: `for`, `do while`, `while` or a statement like `switch`.

break Statement in Java

Java `break` statement is used in loops like `while`, `for-loop`, `do-while`, and switch statement and when used in such statements, it will cause to break and terminate the target loop and the execution engine will move on to the next instructions after the body of the target loop (or the switch statement if the break is used in that area).

Java break statement Syntax:

break;

As you can see, the statement does not take a value.

Example: for loop break statement

public class Simple {
    public static void main(String[] args) {
        for (int i = 0; i<10; i++){
            if(i>5){
                break;
            }
            System.out.print(i+", ");
        }
        System.out.println("\nDone!");
    }
}
Output: 
0, 1, 2, 3, 4, 5, 
Done!

Here, the `for` loop supposed to loop through 10 elements. But because there’s a `break` statement that will be triggered (in the `if` statement) after the value of the `i` variable becomes 6, the loop breaks at this point and the instructions after the loop run. In this case the instruction is just a simple message to the screen. 

Example: while loop break statement

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
		int num = 0; 
		while (num<100){
			if (num>5){
				break;
			}
			num++; 
		}
		System.out.println(num);
	}
}
Output: 
6

Example: do-while break statement

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
		int num = 0; 
		do{
			if (num>5){
				break;
			}
			num++; 
		}
		while (num<100);
		System.out.println(num);
	}
}
Output: 
6

Java break Statement in nested loop

The `break` statement could also be used inside a nested loop as well. Now the important thing to remember here is that when using the `return` statement in a nested loop, only that loop will terminate! This means the enclosing or parent loop will continue to run its iteration.

Example: nested loop and break statement

import java.util.Arrays;
class Main{
	public static void main(String[] args) {
		int num = 0; 
		for (int i = 0; i<3;i++){
			for (int b = 0; b<100;b++){
				if (b>5){
					break;
				}
				System.out.println("The value of the b variable is"+b);
			}
			System.out.println("The value of the i variable is: "+i);
		}
	}
}
Output: 
The value of the b variable is0
The value of the b variable is1
The value of the b variable is2
The value of the b variable is3
The value of the b variable is4
The value of the b variable is5
The value of the i variable is: 0
The value of the b variable is0
The value of the b variable is1
The value of the b variable is2
The value of the b variable is3
The value of the b variable is4
The value of the b variable is5
The value of the i variable is: 1
The value of the b variable is0
The value of the b variable is1
The value of the b variable is2
The value of the b variable is3
The value of the b variable is4
The value of the b variable is5
The value of the i variable is: 2

Difference between break and continue (Break vs Continue)

There’s another statement called `continue` that usually hear about when learning the `break` statement.

The main difference between the break and continue statements is that:

  • The break statement will terminate the execution of a loop entirely!
  • But the continue statement will simply terminate the current iteration of the loop and the execution engine will move to the next iteration. So basically, we’re still in the loop and only the current iteration is skipped.

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies