Break Statement in Java

Break statements are used to terminate the loop immediately, based on the condition and continues to executes the following code to the loop.

Break statements are used in the switch statements and loops such as for, while & do while loop. As break terminates on condition, break should be added in if condition.

Syntax

break;
or
break labelname;


Break statement in Java

package com.yawintutor;

public class BreakStatement {
	public static void main(String[] args) {
		
		for (int i = 0; i < 10; i++) {
			if(i==4) {
				break;
			}
			System.out.println(i);
		}
	}
}

Output

0
1
2
3


Break statement in Nested For loop in Java

If a break is added with in a nested for loop, the break will terminate the inner for loop and continue to execute the next statement within outer for loop. Below is an example of how the break works in the loop nested.

package com.yawintutor;

public class NestedBreak {
	public static void main(String[] args) {
		
		for (int i = 1; i <= 3; i++) {
			System.out.println("\nSTART OUTER FOR i = " + i);

			for (int j = 1; j <= 10; j++) {
				if(j==3) {
					break;
				}
				System.out.println("i = " + i + " j = " + j);
			}
			System.out.println("END   OUTER FOR i = " + i);
		}
	}
}

Output


START OUTER FOR i = 1
i = 1 j = 1
i = 1 j = 2
END   OUTER FOR i = 1

START OUTER FOR i = 2
i = 2 j = 1
i = 2 j = 2
END   OUTER FOR i = 2

START OUTER FOR i = 3
i = 3 j = 1
i = 3 j = 2
END   OUTER FOR i = 3


Labeled Break statement in Java

The labeled break statement terminates both inner and outer for loop. labeled break statement executes the next statement following to the outer for loop.

package com.yawintutor;

public class LabeledBreak {
	public static void main(String[] args) {
		
		outer:
		for (int i = 1; i <= 3; i++) {
			System.out.println("\nSTART OUTER FOR i = " + i);

			for (int j = 1; j <= 5; j++) {
				if(i==2 && j==3) {
					break outer;
				}
				System.out.println("i = " + i + " j = " + j);
			}
			System.out.println("END   OUTER FOR i = " + i);
		}
	}
}

Output


START OUTER FOR i = 1
i = 1 j = 1
i = 1 j = 2
i = 1 j = 3
i = 1 j = 4
i = 1 j = 5
END   OUTER FOR i = 1

START OUTER FOR i = 2
i = 2 j = 1
i = 2 j = 2


Two Labeled Break statement in Java

If the program needs to terminate loops to the immediate level of for loop, then we can use multiple labeled break statements as shown below.

package com.yawintutor;

public class TwoLabeledBreak {
	public static void main(String[] args) {

		outer: for (int i = 1; i <= 3; i++) {
			System.out.println("\nSTART OUTER FOR i = " + i);

			for (int j = 1; j <= 3; j++) {

				if (i == 2 && j == 2) {
					break outer;
				}

				middle: for (int k = 1; k <= 5; k++) {

					if (k == 3) {
						break middle;
					}
					System.out.println("i = " + i + " j = " + j + " k = " + k);
				}
			}
			System.out.println("END   OUTER FOR i = " + i);
		}
	}
}

Output


START OUTER FOR i = 1
i = 1 j = 1 k = 1
i = 1 j = 1 k = 2
i = 1 j = 2 k = 1
i = 1 j = 2 k = 2
i = 1 j = 3 k = 1
i = 1 j = 3 k = 2
END   OUTER FOR i = 1

START OUTER FOR i = 2
i = 2 j = 1 k = 1
i = 2 j = 1 k = 2


Break Statement with while loop in Java

The example below shows how to use the break statement in while loop.

package com.yawintutor;

public class BreakWithWhileLoop {
	public static void main(String[] args) {
		int i = 0;
		while (i < 10) {
			if(i==5) {
				break;
			}
			System.out.println(i);
			i++;
		}
	}
}

Output

0
1
2
3
4


Break Statement with do while loop in Java

The following example shows how to use the break statement in do while loop.

package com.yawintutor;

public class BreakWithDoWhileLoop {
	public static void main(String[] args) {
		int i = 0;
		do {
			if(i==5) {
				break;
			}
			System.out.println(i);
			i++;
		} while (i < 10);
	}
}

Output

0
1
2
3
4



Related Articles

Leave a Reply

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