1. Introduction
It often happens that you don’t need to iterate a loop to the end and instead need to exit early—as soon as you find the desired element, for example. In other cases, you need to skip part of the work and move on to the next step (for example, you read data and want to skip its processing). This is flow control inside a loop.
The break and continue statements in Java are designed exactly for this. Let’s figure out how they work and where they are useful in real tasks.
2. The break statement: “Stop right there!”
Theory and syntax
The break statement is used to exit a loop immediately, wherever you are inside its body. As soon as program execution reaches break, the loop stops right away, and execution continues with the first line of code after the loop.
while (condition)
{
if (kakoe-to_condition)
{
break;
}
// the rest of the loop code
}
The same applies to for and do-while:
for (int i = 0; i < 10; i++)
{
if (kakoe-to_condition)
{
break;
}
// the rest of the loop code
}
Example 1: checking a number for primality
A number is prime if it is divisible only by 1 and itself. We need to check a number for primality: iterate over all potential divisors from 2 to n-1. If it turns out that our number is divisible by a divisor, there’s no need to check further—the number is not prime! We can end the loop.
Example:
int number = 111;
boolean found = false;
for (int i = 2; i < number; i++)
{
if (number % i == 0)
{
found = true;
System.out.println("Found a divisor " + i );
break; // Stop the loop; no need to search for anything else!
}
}
if (!found)
{
System.out.println("No divisor found - the number is prime.");
}
Example 2: user input validation
Let’s write a small app, for example an interactive form to collect data:
while (true)
{
System.out.print("Enter your age: ");
if (console.hasNextInt())
{
int age = console.nextInt();
if (age > 0)
{
System.out.println("Great! Your age: " + age);
break; // data is valid - exiting
}
else
{
System.out.println("Error! Enter a positive age.");
}
}
else
{
System.out.println("Error! Enter a number.");
console.nextLine(); // consume invalid input
}
}
Why this is useful:
Sometimes you need to run a loop indefinitely (or for a long time) until the user enters valid data. As soon as the event we want occurs, we use break and exit the loop for good.
3. The continue statement: “Next, please!”
Theory and syntax
Unlike break, the continue statement does not terminate the entire loop; it only interrupts the current iteration: everything written after continue in the loop body does NOT execute—the loop moves on to the next iteration.
for (int i = 0; i < 10; i++)
{
if (condition)
{
continue;
}
// this part runs only if continue did not trigger
}
Example 3: skipping even numbers
Imagine you need to print only odd numbers from a range. Even ones should be skipped.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue; // if the number is even - skip everything below!
}
System.out.println("Odd number: " + i);
}
What’s happening:
When i = 2, 4, 6... the loop encounters continue and immediately jumps to the next iteration without executing System.out.println.
Step-by-step visualisation:
| i | i % 2 == 0 | Action |
|---|---|---|
| 1 | false | print, no continue |
| 2 | true | continue, no print |
| 3 | false | print, no continue |
| ... | ... | ... |
The break and continue statements are used very often—practically in every other loop. You’ll enjoy working with them.
GO TO FULL VERSION