Java Break/Continue

Controlling loop execution flow with break and continue statements

🛑 What are Break and Continue?

Break and continue statements control loop execution. Break exits the loop completely, while continue skips the current iteration and moves to the next one, giving you precise control over loop behavior.


// Break example - stops at 3
for (int i = 1; i <= 5; i++) {
    if (i == 3) break;
    System.out.println(i);
}
                                    

Output:

1
2

Break vs Continue

🛑

Break Statement

Exits the loop completely

if (condition) break;
⏭️

Continue Statement

Skips current iteration only

if (condition) continue;
🔄

Loop Control

Both work in for, while loops

for/while loops
🎯

Conditional Use

Usually used with if statements

if (x == 5) break;

🔹 Break Statement

The break statement immediately exits the loop:

// Find first number divisible by 7
for (int i = 1; i <= 20; i++) {
    if (i % 7 == 0) {
        System.out.println("Found: " + i);
        break; // Exit loop when found
    }
    System.out.println("Checking: " + i);
}

Output:

Checking: 1
Checking: 2
Checking: 3
Checking: 4
Checking: 5
Checking: 6
Found: 7

🔹 Continue Statement

The continue statement skips the rest of the current iteration:

// Print only odd numbers
for (int i = 1; i <= 8; i++) {
    if (i % 2 == 0) {
        continue; // Skip even numbers
    }
    System.out.println("Odd number: " + i);
}

Output:

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

🔹 Practical Examples

🔸 Password Validation

// Simulate password attempts (max 3 tries)
String correctPassword = "java123";
String[] attempts = {"wrong1", "wrong2", "java123", "wrong3"};

for (int i = 0; i < attempts.length; i++) {
    System.out.println("Attempt " + (i+1) + ": " + attempts[i]);
    
    if (attempts[i].equals(correctPassword)) {
        System.out.println("Access granted!");
        break; // Stop trying once correct
    }
    
    if (i == 2) { // Max 3 attempts
        System.out.println("Too many failed attempts!");
        break;
    }
}

Output:

Attempt 1: wrong1
Attempt 2: wrong2
Attempt 3: java123
Access granted!

🔸 Skip Negative Numbers

// Process only positive numbers
int[] numbers = {5, -2, 8, -1, 3, -4, 7};

for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] < 0) {
        System.out.println("Skipping negative: " + numbers[i]);
        continue; // Skip negative numbers
    }
    
    int square = numbers[i] * numbers[i];
    System.out.println(numbers[i] + " squared = " + square);
}

Output:

5 squared = 25
Skipping negative: -2
8 squared = 64
Skipping negative: -1
3 squared = 9
Skipping negative: -4
7 squared = 49

🔹 Break vs Continue Comparison

See the difference side by side:

With Break (stops at 4):

for (int i = 1; i <= 6; i++) {
    if (i == 4) break;
    System.out.println(i);
}
// Output: 1, 2, 3

With Continue (skips 4):

for (int i = 1; i <= 6; i++) {
    if (i == 4) continue;
    System.out.println(i);
}
// Output: 1, 2, 3, 5, 6

🧠 Test Your Knowledge

What happens when you use 'continue' in a loop?