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 statements 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 statements. In these situations, we can use this Java Break statement and Continue statements.

Java Break Statement

The Java Break statement is very useful to exit from any loop, such as For Loop, While Loop, and Do While Loop. While executing these loops, 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 statements 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 Statement to check for the condition and place the Break statement inside the If block. If the condition is True, then Javac will execute the break statement and, the Java break statement will exit the controller from the loop entirely. Otherwise, it will execute all the statements.

The Java Break statement is the most important statement 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 break statement inside the for loop to exit from loop iteration.

// Java Break statement example 

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 Java Break Statement in For loop 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 For loop, While loop, Do While Loop, If Statement, and Continue statements articles in Java Programming.

Inside the For loop, we placed the If statement 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 the Break statement will execute, and the iteration will stop at that number without printing the System.out.format(“%d”, i) statement.

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.

// Java Break statement example 
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 loop Where i = %d", i);
				break;
			}
			System.out.format(" %d  ",i);
			i--;
		}
	}
}
 10   9   8   7   6   5  
 Coming out from While loop Where i = 4

Within this 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 loop, we placed if statement to test whether i is equal to 4.

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

Comments are closed.