Learn about the Java Break statement with the help of practical programming examples of how to use Break in Java programs:
In this tutorial, we will discuss another Jump statement of Java i.e. break statements.
We will explain the break statement with description, programming examples, and see how to use break inside various loops and Switch statements, etc.
=> Explore The Simple Java Training Series Here
Table of Contents:
Java Break Statement
Unlike Java continue, the break statement in Java is used to immediately terminate the loop without executing the remaining part of the body of the loop. Even though it is not mandatory, it is mostly used inside a Switch statement.
Exiting A Loop Using A Break In Java
In this example, we have taken a “for loop” and demonstrated how to jump out of the loop using a break statement.
This is the most simple and common example of a break statement.
public class example { public static void main(String[] args) { for (int i = 0; i < 20; i++) { if (i == 6) { break; } System.out.println(i); } } }
Output
Labeled Break With A Nested Loop
In this type of break, we provide the name of the label that identifies the code block.
Syntax:
break label;
Whenever this kind of break executes, the control is transferred out of the block i.e. it bypasses the execution of any following statement.
Here, we are demonstrating how the labeled break statement behaves in a nested loop. A nested loop is nothing but the loop inside a loop. The first loop that appears is known as the outer loop and the loops that are inside the first loop are called the inner loop.
In this example, when the inner loop ‘j’ breaks to the outer loop ‘i’, both the loops are terminated. This means that ‘i’ is iterated only once whereas ‘j’ is iterated 5 times i.e. from 0 to 4.
class example { public static void main(String args[]) { outer: for (int i = 0; i < 3; i++) { System.out.print("For i = " + i + ": "); System.out.println("j value is: "); /* * When the inner loop 'j' breaks to the outer loop * 'i', both the loops are terminated. This means * that the 'i' is iterated only once whereas the 'j' * is iterated 5 times i.e. from 0 to 4. */ for (int j = 0; j < 100; j++) { if (j == 5) break outer; System.out.println(" " + j); } } System.out.println("Loops terminated"); } }
Output
Break Java With A While Loop
In the below example, we have been incrementing the value of ‘i’ that is initialized with the value 5. As soon as the value becomes 10, the control goes inside the if statement inside the while loop and the loop gets terminated.
class example { public static void main(String args[]) { int i = 5; // while loop starts here while (i <= 25) { System.out.println(i); i = i + 1; // as soon as 'i' becomes 10, the loop ends. if (i == 10) { break; } } } }
Output
Break Java With A Switch
In this example, we will demonstrate how to use a break statement inside a “Switch statement”. As we know, when a break statement is encountered the Switch gets terminated. The control of the program moves to the code/statement following the Switch statement.
Even though it is optional, we can also use a break inside every Switch case. In the below program, see the switch (a) -> case 2. If we remove the break then case 3 will also be executed. So, the break helps in maintaining the isolation of different Switch cases from each other.
import java.util.Scanner; public class example { public static void main(String[] args) { int a, b; System.out.println("Enter a and b"); Scanner in = new Scanner(System.in); a = in.nextInt(); b = in.nextInt(); // Outer Switch starts here switch (a) { // If a = 1 case 1: // Inner Switch starts here switch (b) { // for condition b = 1 case 1: System.out.println("b is 1"); break; // for condition b = 2 case 2: System.out.println("b is 2"); break; // for condition b = 3 case 3: System.out.println("b is 3"); break; } break; // for condition a = 2 case 2: System.out.println("a is 2"); /* * if we remove this break then case 3 will also be executed * for a = 2 */ break; // for condition a == 3 case 3: System.out.println("a is 3"); break; default: System.out.println("default statement here"); break; } } }
Output
Also read =>> MySQL CASE Statement Tutorial
Frequently Asked Questions
Q #1) Is it bad to use break Java?
Answer: Not at all. There are many usages of a Java break. The best use can be seen in a Switch statement to maintain the isolation of different Switch cases from each other. Another use of break is to come out of the loop as soon as it meets any specified/predefined condition.
Q #2) How do you break a Java program?
Answer: The simplest way of using a break inside a Java program is to initialize a loop followed by specifying certain conditions using the if statement and then the break statement inside the if condition.
However, if you explicitly want your Java program to break then you can use “System. exit(0);”
Q #3) Does Break Break Out of all loops Java?
Answer: No. It only breaks the current loop in which the break statement is specified.
Q #4) Can Break be used in if statement?
Answer: Yes, you can specify the break statement inside the if statement with some conditions.
For example:
for (int i = 0; i < 10; i++) { if (i == 6) { break;
Conclusion
In this tutorial, we have explained the Java break statement i.e. the most convenient jump statement. We have properly described the statement along with examples and programs wherever required. Some FAQ’s have been provided for better understanding.
Moreover, we have tried listing the examples of a break statement with different types of loops and conditional statements that will provide you a better understanding of this jump statement.
=> Visit Here To Learn Java From Scratch