Introduction to Break and Continue
In Java, loops usually follow a fixed flow: they start, check a condition, execute the body, and then repeat until the condition becomes false.
However, sometimes we want to change this normal flow, like stopping the loop early or skipping certain iterations.
To handle such situations, Java provides two powerful control statements: break and continue.
These statements make your code more flexible, efficient, and logical, especially when working with loops or switch cases.
1) The break Statement
The break statement is used when you want to immediately stop the execution of a loop or a switch block, even if the loop’s condition is still true.
Once a break is encountered, the program jumps out of the loop or switch and continues executing the code that follows it.
For example:
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
System.out.println("Loop stopped at: " + i);
break; // Exit the loop when i becomes 5
}
System.out.println("Number: " + i);
}
}
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Loop stopped at: 5
- Here, the loop was supposed to run 10 times, but the break statement stopped it early when i reached 5.
2) The continue Statement
The continue statement doesn’t stop the entire loop; instead, it skips the current iteration and jumps to the next one.
This is helpful when you want to ignore specific values or conditions without breaking the loop completely.
For example:
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip printing when i is 3
}
System.out.println("Value: " + i);
}
}
}
Output:
Value: 1
Value: 2
Value: 4
Value: 5
- In this code, when i becomes 3, the continue statement skips that iteration, so “Value: 3” is never printed, but the loop continues running for other numbers.
Example 2: Continue in a While Loop
In a while loop, the continue statement is used when you want to skip certain iterations without stopping the loop completely.
public class SkipMultiplesExample {
public static void main(String[] args) {
int number = 1;
while (number <= 10) {
if (number % 3 == 0) {
number++; // Increase number before skipping to avoid infinite loop
continue; // Skip printing if number is a multiple of 3
}
System.out.println("Current Number: " + number);
number++;
}
}
}
Output:
Current Number: 1
Current Number: 2
Current Number: 4
Current Number: 5
Current Number: 7
Current Number: 8
Current Number: 10
Explanation of this code:
- The loop starts from 1 and runs till 10.
- Inside the loop, we check whether the number is a multiple of 3 (number % 3 == 0).
- If it is, we skip printing that number using continue.
- The loop then moves on to the next number.
- This way, numbers like 3, 6, and 9 are skipped automatically.
Advanced Example: Combining Break and Continue
In this Java program, both break and continue statements are used together inside a for loop to control how the loop behaves.
Code example:
public class BreakContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 3) {
System.out.println("Skipping number 3");
continue; // Skip the iteration when i is 3
}
if (i == 7) {
System.out.println("Breaking the loop at number 7");
break; // Stop the loop completely when i is 7
}
System.out.println("Number: " + i);
}
}
}
Output:
Number: 1
Number: 2
Skipping number 3
Number: 4
Number: 5
Number: 6
Breaking the loop at number 7
- The continue statement tells Java: “Skip this number and move to the next one.”
So when i == 3, the program doesn’t print Number: 3. It just skips to i = 4. - The break statement tells Java: “Stop this loop completely.”
When i == 7, the program ends the loop immediately, so numbers after 7 (like 8, 9, 10) are never printed.
Also Learn Important Topics of Java
- What are the operators in Java Programming?
- What are Data Types in Java?
- What are the Variables in Java?
- Learn Java syntax.
- What is Java if else statements?

M.Sc. (Information Technology). I explain AI, AGI, Programming and future technologies in simple language. Founder of BoxOfLearn.com.