A Java break statement halts the execution of a loop. When a break statement is run, a program starts to run the code after a statement. If a break statement is used in a nested loop, only the innermost loop is terminated.
Java for loops and while loops are used to automate similar tasks. When you’re working with these loops, you may want to exit a loop when a particular condition is met. That’s where the Java break statement comes in. The break statement is used to stop a loop completely.
This tutorial will discuss using the break statement to control the flow of your loops in Java. We’ll walk through an example of the break statement in a Java program.
Java break Statement
The Java break statement halts the execution of a loop. The interpreter moves onto the next statement in a program after the loop. The break statement is useful if you want your loop to stop running if a certain condition is met in a loop.
The syntax for the break statement is as follows:
break;
The break statement stands alone as its own keyword. It does not accept any arguments because the break statement is not a function. When a break statement is encountered, the program skips the current iteration of the loop.
break Java Example
Let’s say we are creating a program that asks a user to guess a number between one and ten.
If the user guesses the correct number, our program should print out a message congratulating the user on guessing the right number. Otherwise, our user should be allowed to guess again, up to a total of five guesses.
Here’s the code we could use to write this program:
import java.util.Scanner; class GuessingGame { public static void main(String[] args) { int number = 6; Scanner input = new Scanner(System.in); for (int i = 0; i <= 5; i++) { System.out.print("Guess a number between 1 and 10: "); guess = input.nextInt(); if (guess == number) { System.out.println("You're correct!"); break; } } } }
Our code returns:
Guess a number between 1 and 10: 1 Guess a number between 1 and 10: 6 You're correct!
Our program asks us to guess again if we do not guess the correct number.
If a user has already had five guesses, the program stops. But if a user guesses the correct number, our code prints “You’re correct!” to the console.
Java break Example Breakdown
Let’s break down our code. First, we import the java.util.Scanner library, which allows us to accept user input. We define a class called GuessingGame. Then our program does the following:
- We declare a Java variable called number. This variable stores the number the user should guess.
- We use Scanner input to initialize our user input, so we can retrieve guesses from the user in our program.
- We create a Java for loop which runs until i is greater than five. This for loop gives our user five attempts at guessing the correct number.
- Our code prints “Guess a number between 1 and 10: “ to the console.
- We use input.nextInt() to accept the user’s guess. We store the guess in a new variable called guess.
- Our program compares whether the user’s guess is equal to the number variable, which stores the number our user should guess.
- If guess is equal to number, the message “You’re correct!” is printed to the console. Then, the loop stops executing.
- Otherwise, the for loop will execute until i is greater than 5.
The break statement terminates a for or while loop immediately after the break statement is executed.
Java Nested break Statement
The break statement terminates the innermost loop in a Java program. Say you have a while loop within a for loop, for example, and the break statement is in the while loop. The only loop that will stop is the while loop.
Here’s an example to illustrate how this works:
for (int; expression; updateCounter) { // Code while (true) { // Code if (condition_is_met) { break; } } }
First, we initialize a for loop. Then, we initialize a Java while loop. When our break statement runs, the while loop will stop executing. However, the for loop will keep executing until the program stops.
Java Labelled break Statement
You can assign a label to a label to a break statement and create a labelled break. These are used to terminate the labelled statement in a program, unlike unlabeled break statements that break the innermost loop. Here’s the syntax for a labelled break:
break label_name; For instance, say we have the following nested loop: for (int; expression; updateCounter) { // Code for (int; expression; updateCounter) { // Code while (true) { if (condition_is_met) { break; } } } }
When our program meets a condition, we want our code to break and resume executing the first for loop. In other words, we want our program toterminate the inner loop. The outer loop should keep running.
We could accomplish this by using a labelled break statement. Here’s the code we would use to break out of our second for loop and the while loop:
for (int; expression; updateCounter) { // Code top_break: for (int; expression; updateCounter) { // Code while (true) { if (condition_is_met) { break top_break; } } } }
As soon as the break top_break statement executes, the program terminates all loops until our cod executes the top_break statement. In this case, this means that the second for loop and the while loop are both terminated, and the program continues running.
Conclusion
The Java break statement terminates a loop. The labelled break statement is used to terminate a loop and jump to the statement corresponding to the labelled break.
This tutorial discussed how to use the break and labelled break statements to control the flow of a program.
Do you want to learn more about Java? Check out our complete How to Learn Java guide. In this guide, you’ll find advice on top courses, books, and learning resources.
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.