Java Break & Continue Statements
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Break & Continue Statements

Jeevadharshan

Java break and continue Statements 

Control flow statements like break and continue allow you to alter the normal execution of loops in Java. They help you exit loops early or skip specific iterations, improving efficiency and readability in many scenarios.  

Java break Statement 

The break statement immediately terminates the nearest enclosing loop or switch block and 
transfers control to the statement that follows it. 
 
It is commonly used when a stopping condition is met before the loop finishes naturally. 
 
Example 1: Stopping a Loop When a Condition Is Met 
 
This program prints numbers from 1 onward but stops when it encounters a multiple of 7. 
 
public class BreakExample { 
    public static void main(String[] args) { 
 
        for (int num = 1; num <= 20; num++) { 
            if (num % 7 == 0) { 
                System.out.println("Stopped at: " + num); 
                break; 
            } 
            System.out.println("Number: " + num); 
        } 
 
    } 

Output 

Number: 1 
Number: 2 
Number: 3 
Number: 4 
Number: 5 
Number: 6 
Stopped at: 7

Java continue Statement  

The continue statement skips the current iteration of a loop and proceeds directly to the next iteration. 
 
It is useful when certain values should be ignored without stopping the entire loop. 
 
Example 2: Skipping Specific Values 
 
This program prints numbers from 1 to 10 but skips all even numbers. 
 
public class ContinueExample { 
    public static void main(String[] args) { 
 
        for (int i = 1; i <= 10; i++) { 
            if (i % 2 == 0) { 
                continue; 
            } 
            System.out.println("Odd number: " + i); 
        } 
 
    } 
}  

Output 

Odd number: 1 
Odd number: 3 
Odd number: 5 
Odd number: 7 
Odd number: 9  

Combining break and continue 

You can use both statements within the same loop to skip certain values and stop processing at a defined condition. 
 
Example 3: Skip and Stop in the Same Loop 
 
This program skips multiples of 3 but stops entirely when a number greater than 10 is reached. 
 
public class BreakContinueCombined { 
    public static void main(String[] args) { 
 
        for (int i = 1; i <= 15; i++) { 
 
            if (i % 3 == 0) { 
                continue; // skip multiples of 3 
            } 
 
            if (i > 10) { 
                System.out.println("Stopping at: " + i); 
                break; 
            } 
 
            System.out.println("Processed: " + i); 
        } 
 
    } 
}

Output  

Processed: 1 
Processed: 2 
Processed: 4 
Processed: 5 
Processed: 7 
Processed: 8 
Processed: 10 
Stopping at: 11 

Using break and continue in while Loops  

Both statements work the same way in while loops as in for loops. 
 
Example 4: break in a While Loop 
 
This program generates numbers until a value divisible by 5 is found. 
 
public class WhileBreakExample { 
    public static void main(String[] args) { 
 
        int n = 1; 
 
        while (n <= 20) { 
            if (n % 5 == 0) { 
                System.out.println("Terminated at: " + n); 
                break; 
            } 
            System.out.println(n); 
            n++; 
        } 
 
    } 
 
Example 5: continue in a While Loop 
 
This program prints numbers from 1 to 10 but skips numbers divisible by 4. 
 
public class WhileContinueExample { 
    public static void main(String[] args) { 
 
        int n = 0; 
 
        while (n < 10) { 
            n++; 
 
            if (n % 4 == 0) { 
                continue; 
            } 
 
            System.out.println(n); 
        } 
 
    } 
}

Real-World Example: Filtering and Stopping Conditions

Imagine processing sensor readings where:
  • Negative values → invalid → skip 
  • Zero → critical stop signal → terminate 
  • Positive values → valid → process
public class SensorProcessing { 
    public static void main(String[] args) { 
 
        int[] readings = {5, -2, 8, 3, 0, 6, 9}; 
 
        for (int value : readings) { 
 
            if (value < 0) { 
                continue; // ignore invalid readings 
            } 
 
            if (value == 0) { 
                System.out.println("Critical stop detected."); 
                break; 
            } 
 
            System.out.println("Valid reading: " + value); 
        } 
 
    } 
}  

Output 

Valid reading: 5 
Valid reading: 8 
Valid reading: 3 
Critical stop detected.
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !