Skip to content

Jumping statements

In Java, jumping statements are used to control the flow of execution by transferring control to a specific point within the program. The primary jumping statements are break and continue, each serving a distinct purpose. These statements enhance the flexibility of loops and switch statements.


1. break Statement

  • Purpose: Used to terminate a loop or exit a switch statement immediately.
  • Syntax:

break;

  • Use Cases:
    • Exiting a loop when a specific condition is met.
    • Terminating a switch case to prevent fall-through.

Example in a Loop:

public class BreakExample {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            if (i == 3) {

                break; // Exit the loop when i equals 3

            }

            System.out.println(“Iteration: ” + i);

        }

    }

}

Output:

Iteration: 1

Iteration: 2

Example in a switch Statement:

public class BreakInSwitchExample {

    public static void main(String[] args) {

        int day = 3;

        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”);

        }

    }

}

Output:

Wednesday


2. continue Statement

  • Purpose: Skips the current iteration of a loop and proceeds to the next iteration.
  • Syntax:

continue;

  • Use Cases:
    • Skipping specific iterations in a loop based on a condition.
    • Useful for avoiding complex if-else logic within loops.

Example in a for Loop:

public class ContinueExample {

    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            if (i == 3) {

                continue; // Skip the iteration when i equals 3

            }

            System.out.println(“Iteration: ” + i);

        }

    }

}

Output:

Iteration: 1

Iteration: 2

Iteration: 4

Iteration: 5

Example in a while Loop:

public class ContinueInWhileExample {

    public static void main(String[] args) {

        int i = 1;

        while (i <= 5) {

            i++;

            if (i == 3) {

                continue; // Skip the rest of the code when i equals 3

            }

            System.out.println(“Iteration: ” + i);

        }

    }

}

Output:

Iteration: 2

Iteration: 4

Iteration: 5

Iteration: 6


Comparison of break and continue

Featurebreakcontinue
PurposeExits the loop entirely.Skips the current iteration.
Effect on LoopTerminates the loop execution.Continues to the next iteration.
Use CaseWhen further iterations are not needed.When a specific iteration should be skipped.
ScopeWorks in loops and switch.Works only in loops.

Jump Statements in Nested Loops

Using break in Nested Loops:

  • When used in nested loops, break exits the innermost loop by default.
  • To break out of outer loops, labeled break can be used.

Example of Labeled break:

public class LabeledBreakExample {

    public static void main(String[] args) {

        outer:

        for (int i = 1; i <= 3; i++) {

            for (int j = 1; j <= 3; j++) {

                if (i == 2 && j == 2) {

                    break outer; // Exit both loops

                }

                System.out.println(“i: ” + i + “, j: ” + j);

            }

        }

    }

}

Output:

i: 1, j: 1

i: 1, j: 2

i: 1, j: 3

i: 2, j: 1

Using continue in Nested Loops:

  • When used in nested loops, continue skips the current iteration of the innermost loop.

Example of Labeled continue:

public class LabeledContinueExample {

    public static void main(String[] args) {

        outer:

        for (int i = 1; i <= 3; i++) {

            for (int j = 1; j <= 3; j++) {

                if (i == 2 && j == 2) {

                    continue outer; // Skip to the next iteration of the outer loop

                }

                System.out.println(“i: ” + i + “, j: ” + j);

            }

        }

    }

}

Output:

i: 1, j: 1

i: 1, j: 2

i: 1, j: 3

i: 2, j: 1

i: 3, j: 1

i: 3, j: 2

i: 3, j: 3


Key Points

  1. break:
    1. Use sparingly to avoid abrupt control flow changes.
    1. Preferred in switch statements or when exiting loops early is necessary.
  2. continue:
    1. Helps simplify logic by skipping specific iterations without breaking the loop.
  3. Labeled Statements:
    1. Useful for nested loops but can make code harder to read.
    1. Avoid excessive use of labeled break and continue for better readability.