WikiGalaxy

Personalize

Java Break Statement

Understanding Break:

The break statement in Java is used to terminate a loop or switch statement prematurely. It allows the program to exit the loop or switch block when a certain condition is met.

Use Case:

In a loop, if a specific condition is satisfied, and you want to exit the loop immediately, you use the break statement.


for (int i = 0; i < 10; i++) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
}
        

Explanation:

In this example, the loop will print numbers from 0 to 4. When i becomes 5, the break statement will execute, terminating the loop.

Console Output:

0 1 2 3 4

Java Continue Statement

Understanding Continue:

The continue statement in Java is used to skip the current iteration of a loop and proceed to the next iteration. It can be particularly useful when you want to skip over certain values in a loop.

Use Case:

In a loop, if you want to skip the execution for a particular condition but continue with the next iterations, you use the continue statement.


for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        continue;
    }
    System.out.println(i);
}
        

Explanation:

In this example, the loop will print odd numbers between 0 and 9. When i is even, the continue statement skips the remaining code inside the loop for that iteration.

Console Output:

1 3 5 7 9

Nested Loops with Break

Using Break in Nested Loops:

When dealing with nested loops, the break statement only exits the loop in which it is placed. To exit multiple loops, additional logic is required.

Use Case:

Break is useful in nested loops when you want to exit from the innermost loop based on a condition.


for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j) {
            break;
        }
        System.out.println("i: " + i + ", j: " + j);
    }
}
        

Explanation:

In this example, the inner loop breaks whenever i equals j, so it only prints pairs where i is not equal to j.

Console Output:

i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 2, j: 0 i: 2, j: 1

Using Continue in Nested Loops

Continue in Nested Loops:

The continue statement in nested loops will only skip the current iteration of the innermost loop where it is placed.

Use Case:

Continue is useful when you want to skip certain iterations of an inner loop but continue executing the outer loop.


for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j) {
            continue;
        }
        System.out.println("i: " + i + ", j: " + j);
    }
}
        

Explanation:

In this example, the inner loop skips printing when i equals j, so it only prints pairs where i is not equal to j.

Console Output:

i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 1, j: 2 i: 2, j: 0 i: 2, j: 1

Break with Labels

Understanding Break with Labels:

The break statement can be used with labels to terminate outer loops. This is useful when you want to exit multiple nested loops at once.

Use Case:

Labels are particularly useful when you want to break out of a loop that is not the innermost one.


outerLoop:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i + j == 3) {
            break outerLoop;
        }
        System.out.println("i: " + i + ", j: " + j);
    }
}
        

Explanation:

In this example, the label 'outerLoop' allows the break statement to exit both loops when i + j equals 3.

Console Output:

i: 0, j: 0 i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 1, j: 1

Continue with Labels

Understanding Continue with Labels:

The continue statement can also be used with labels to skip iterations of outer loops. This is useful for skipping the rest of the current iteration of a labeled loop.

Use Case:

Labels with continue are useful when you want to skip the current iteration of an outer loop.


outerLoop:
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == j) {
            continue outerLoop;
        }
        System.out.println("i: " + i + ", j: " + j);
    }
}
        

Explanation:

In this example, the label 'outerLoop' allows the continue statement to skip to the next iteration of the outer loop when i equals j.

Console Output:

i: 0, j: 1 i: 0, j: 2 i: 1, j: 0 i: 2, j: 0 i: 2, j: 1

Break in Switch Statements

Using Break in Switch:

The break statement is used in switch statements to terminate a case and prevent fall-through to subsequent cases.

Use Case:

In switch statements, break is crucial to ensure that only the matched case is executed.


int day = 2;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Invalid day");
}
        

Explanation:

In this example, when day is 2, the program prints "Tuesday" and exits the switch block due to the break statement.

Console Output:

Tuesday

Continue in For-Each Loop

Using Continue in For-Each:

The continue statement can be used in for-each loops to skip the current iteration and move to the next element.

Use Case:

In for-each loops, continue is useful when you want to skip processing certain elements.


int[] numbers = {1, 2, 3, 4, 5};
for (int num : numbers) {
    if (num % 2 == 0) {
        continue;
    }
    System.out.println(num);
}
        

Explanation:

In this example, the loop prints only the odd numbers in the array, skipping the even numbers due to the continue statement.

Console Output:

1 3 5

Break in While Loop

Using Break in While Loop:

The break statement can be used in while loops to exit the loop when a specific condition is met.

Use Case:

Break in while loops is useful to stop the loop based on dynamic conditions.


int i = 0;
while (i < 10) {
    if (i == 5) {
        break;
    }
    System.out.println(i);
    i++;
}
        

Explanation:

In this example, the loop prints numbers from 0 to 4. When i becomes 5, the break statement terminates the loop.

Console Output:

0 1 2 3 4

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025