Java break Statement

The Java break and continue are two important statements used to alter the flow of a program in any programming language.

In Java Programming, Loops are useful to perform a specific block of code for n number of times until the test condition is false. There will be some circumstances where we have to stop the loop without completing all the lies of code. In these situations, we can use this Java break and continue statements.

Java break Statement

The Java break statement is very useful to exit from any loop, such as For, While, and Do While. While executing them, if the Javac compiler finds the break statement inside them, it will stop executing the statements and immediately exit from the loop.

For example, we have ten lines of code inside the loop, and we want to exit from the loop when a specific condition is True; otherwise, it has to perform all of them. In this situation, we can use If clause to check for the condition and place the Java break statement inside the If block. If the condition is True, then Javac will execute the break, and it will exit the controller from the loop entirely. Otherwise, it will execute all the lines of code.

The Java Break statement is the most important one in Switch Case. Without using the Java Break, the compiler will not exit from the switch cases. The syntax of the Break Statement in Java Programming language is as follows:

break;

Java break Statement examples

We want to share two examples to display the working functionality of the Break statement in both For loop and While loop.

Java For Loop break Statement

In this program, we use the Java break statement inside the for loop to exit from loop iteration.

package ConditionalStatements;

public class BreakStatement {

	public static void main(String[] args) {
		int i;
		for (i = 0; i <= 10; i++)  {
			if(i == 7)  {
				System.out.format("\n Coming out from for loop Where i = %d\n", i);
				break;
			}
			System.out.format(" %d  ",i);
		}
	}
}
Java Break Statement 0

In this example, First, We initialized the value of i as: i = 10 at the beginning of the code. Within the For loop, we initialized the value of i as: i = 0, and the condition will check whether i is less than or equal to 10. Lastly, we used the Increment Operator to increment the value by 1.

Please refer to the For, While, Do While, If condition, and Continue articles in Java Programming to understand them.

Inside the For, we placed the If condition to test whether i is equal to 7.

  • If the condition is false, then it will skip the Break statement and prints that number as output (In Our case 0, 1, 2, 3, 4, 5, 6).
  • If this condition is True, then it will execute, and the iteration will stop at that number without printing the System.out.format(“%d”, i).

Java While Loop break Statement

In this program, We are going to use the Java break statement inside the While loop to exit from the loop iteration.

package ConditionalStatements;

public class BreakinWhile {

	public static void main(String[] args) {
		int i = 10;
		
		while ( i > 0)  {
			if(i == 4)  {
				System.out.format("\n Coming out from While Where i = %d", i);
				break;
			}
			System.out.format(" %d  ",i);
			i--;
		}
	}
}
 10   9   8   7   6   5  
 Coming out from While Where i = 4

Within this Java Break Statement example, First, We initialized the value of i as: i = 10 at the beginning of the code. Within the While loop, we check for the condition whether i is greater than 0 or not.

while ( i > 0)  {

Inside the While, we placed if condition to test whether i is equal to 4.

  • If the condition is false, then it will skip it and prints that number as output (In Our case 10, 9, 8, 7, 6, 5).
  • If this condition is True, then the statement will execute, and the iteration will stop at that number without printing the System.out.format(“%d”, i).

Comments are closed.