Java Interview Questions

30 Questions
Java

Java

Web DevelopmentBackend

Question 11

Explain the use of the 'break' and 'continue' statements.

Answer:

In Java, the break and continue statements are used to control the flow of loops and switch statements. These statements provide a way to exit or skip parts of the loop or switch case based on specific conditions.

break Statement

Definition: The break statement is used to terminate the loop or switch statement and transfer control to the statement immediately following the loop or switch.

Usage:

  • In Loops: When a certain condition is met, and you want to exit the loop immediately.
  • In Switch Statements: To terminate a case in the switch statement and prevent fall-through to the subsequent cases.

Example in a Loop:

public class BreakExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                break; // Exit the loop when i is 5
            }
            System.out.println(i);
        }
    }
}

Output:

0
1
2
3
4

Example in a Switch Statement:

public class BreakInSwitchExample {
    public static void main(String[] args) {
        int day = 3;
        String dayName;
        switch (day) {
            case 1:
                dayName = "Sunday";
                break;
            case 2:
                dayName = "Monday";
                break;
            case 3:
                dayName = "Tuesday";
                break;
            default:
                dayName = "Invalid day";
                break;
        }
        System.out.println("The day is " + dayName);
    }
}

Output:

The day is Tuesday

continue Statement

Definition: The continue statement is used to skip the current iteration of the loop and proceed with the next iteration.

Usage:

  • In Loops: When a certain condition is met, and you want to skip the remaining code in the current iteration and move to the next iteration.

Example in a Loop:

public class ContinueExample {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                continue; // Skip the current iteration when i is even
            }
            System.out.println(i);
        }
    }
}

Output:

1
3
5
7
9

Key Differences

  1. Purpose:

    • break: Exits the loop or switch statement entirely.
    • continue: Skips the current iteration of the loop and continues with the next iteration.
  2. Usage Context:

    • break: Can be used in both loops and switch statements.
    • continue: Used only in loops.
  3. Control Flow:

    • break: Transfers control to the statement immediately following the loop or switch.
    • continue: Transfers control to the next iteration of the loop.

Practical Use Cases

  • break: Useful for terminating a loop early when a specific condition is met, such as finding an element in a collection and stopping the search once the element is found.
  • continue: Useful for skipping certain iterations based on a condition, such as skipping even numbers in a loop that processes only odd numbers.

Summary

The break and continue statements in Java are powerful tools for controlling the flow of loops and switch statements. By using break, you can exit a loop or switch statement early based on specific conditions. By using continue, you can skip the current iteration of a loop and proceed with the next one, which is helpful for bypassing specific conditions within loops.

Recent job openings