Welcome to The Coding College! In this tutorial, we’ll explore the break
and continue
statements in Java. These statements allow you to control the flow of loops, making your code more efficient and flexible.
Break Statement
The break
statement is used to exit a loop prematurely, regardless of the loop’s condition. It immediately terminates the loop and moves control to the next statement after the loop.
Syntax
break;
Example 1: Exiting a Loop Early
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit loop when i is 5
}
System.out.println(i);
}
System.out.println("Loop exited.");
}
}
Output
1
2
3
4
Loop exited.
Explanation: The loop stops when i
equals 5, thanks to the break
statement.
Example 2: Breaking Out of a Nested Loop
public class NestedBreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
break; // Exit inner loop when j is 2
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1
Explanation: The inner loop stops whenever j
equals 2, but the outer loop continues.
Continue Statement
The continue
statement is used to skip the current iteration of a loop and proceed with the next iteration.
Syntax
continue;
Example 1: Skipping Iterations
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the iteration when i is 5
}
System.out.println(i);
}
}
}
Output
1
2
3
4
6
7
8
9
10
Explanation: The loop skips the iteration when i
equals 5, but it doesn’t terminate the loop.
Example 2: Skipping Odd Numbers
public class SkipOddNumbers {
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i % 2 != 0) {
continue; // Skip odd numbers
}
System.out.println(i);
}
}
}
Output
2
4
6
8
10
Explanation: The loop skips all odd numbers and prints only even numbers.
Example 3: Using Continue in a Nested Loop
public class NestedContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
if (j == 2) {
continue; // Skip iteration when j is 2
}
System.out.println("i = " + i + ", j = " + j);
}
}
}
}
Output
i = 1, j = 1
i = 1, j = 3
i = 2, j = 1
i = 2, j = 3
i = 3, j = 1
i = 3, j = 3
Explanation: The inner loop skips the iteration when j
equals 2, but the outer loop remains unaffected.
Break vs. Continue
Feature | Break | Continue |
---|---|---|
Functionality | Exits the loop entirely. | Skips the current iteration. |
Usage | Used when exiting is necessary. | Used when skipping is needed. |
Impact | Terminates loop execution. | Proceeds with the next iteration. |
Practice Problems
- Write a program to find the first number divisible by 7 in a range and exit the loop once found.
- Create a loop that skips numbers divisible by 3 but prints all others within a range.
- Use nested loops to print a pattern, skipping specific rows or columns based on conditions.
For more Java tutorials and coding tips, visit The Coding College. Stay curious and keep coding! 🚀