Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java break Statement: Break Out Of Loop

The break statement in Java is used to terminate the execution of a loop (for, while, or do-while) or a switch statement. When the break statement is encountered, the control jumps out of the loop or switch block and continues with the next statement after the loop or switch.

In this tutorial, we will go through examples of using the break statement in loops and switch statements.

  • Using break in a loop:

Consider the following example, where we search for the first occurrence of a specific value in an array:

int[] numbers = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
int searchValue = 12;

for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] == searchValue) {
        System.out.println("Found " + searchValue + " at index " + i);
        break; // Terminates the loop once the value is found
    }
}

In this example, when the search value is found, the break statement terminates the loop, and the program continues with the next statement after the loop (if there is any).

  • Using break in a switch statement:

The break statement is also used in switch statements to exit a specific case block. When a case block is executed, the break statement prevents the program from falling through to the next case block.

int dayOfWeek = 3;
String dayString;

switch (dayOfWeek) {
    case 1:
        dayString = "Monday";
        break;
    case 2:
        dayString = "Tuesday";
        break;
    case 3:
        dayString = "Wednesday";
        break;
    case 4:
        dayString = "Thursday";
        break;
    case 5:
        dayString = "Friday";
        break;
    case 6:
        dayString = "Saturday";
        break;
    case 7:
        dayString = "Sunday";
        break;
    default:
        dayString = "Invalid day";
        break;
}

System.out.println(dayString);

In this example, the break statement is used to exit the case block when a specific day of the week is found. If the break statement is not used, the program will fall through to the next case block and continue executing until it encounters a break statement or reaches the end of the switch block.

These are the primary use cases for the break statement in Java. The break statement helps to control the flow of execution in loops and switch statements, making it an essential tool in various programming scenarios.

  1. Using Break Statement to Exit Loops in Java: The break statement is used to exit a loop prematurely.

    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break;
        }
        System.out.println(i);
    }
    
  2. Breaking Out of a Loop Early in Java: The break statement allows breaking out of a loop based on a condition.

    while (true) {
        // Some condition
        if (conditionMet) {
            break;
        }
        // Loop body
    }
    
  3. Java Labeled Break Statement: Labeled break allows breaking out of nested loops based on a label.

    outerLoop:
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (someCondition) {
                break outerLoop;
            }
            // Loop body
        }
    }
    
  4. Nested Loops and Break in Java: break can be used to exit nested loops.

    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (someCondition) {
                break;
            }
            // Loop body
        }
    }
    
  5. Java Break Statement vs. Continue Statement: break exits the loop, while continue skips the current iteration.

    for (int i = 0; i < 10; i++) {
        if (someCondition) {
            break; // or continue;
        }
        // Loop body
    }
    
  6. Conditional Use of Break in Java Loops: Use break based on a specific condition.

    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break;
        }
        // Loop body
    }
    
  7. Infinite Loop with Break in Java: Create an infinite loop with a break condition.

    while (true) {
        // Some condition
        if (breakCondition) {
            break;
        }
        // Loop body
    }
    
  8. Using Break in Switch Statements in Java: break is used to exit a switch statement.

    switch (value) {
        case 1:
            // Case body
            break;
        case 2:
            // Case body
            break;
        // Other cases
    }
    
  9. Java Break Statement with Labeled Blocks: break can be used with labeled blocks.

    label: {
        // Block body
        if (someCondition) {
            break label;
        }
        // More block body
    }
    
  10. Common Patterns for Using Break in Java: Common patterns include breaking out of loops based on certain conditions.

    for (int i = 0; i < array.length; i++) {
        if (array[i] == targetValue) {
            break;
        }
        // Loop body
    }
    
  11. Java Break Statement with While Loop: break works similarly in a while loop.

    int i = 0;
    while (i < 10) {
        if (someCondition) {
            break;
        }
        // Loop body
        i++;
    }
    
  12. Exiting Multiple Loops with Break in Java: Labeled break allows exiting multiple loops simultaneously.

    outerLoop:
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (someCondition) {
                break outerLoop;
            }
            // Loop body
        }
    }
    
  13. Java For Loop with Break Statement: Using break in a for loop.

    for (int i = 0; i < 10; i++) {
        if (i == 5) {
            break;
        }
        // Loop body
    }
    
  14. Handling Exceptions with Break in Java: break can be used in exception handling scenarios.

    try {
        // Some code
        if (someCondition) {
            break;
        }
        // More code
    } catch (Exception e) {
        // Exception handling
    }
    
  15. Loop Termination Conditions and Break in Java: Use break to terminate loops based on specific conditions.

    while (true) {
        if (terminationCondition) {
            break;
        }
        // Loop body
    }
    
  16. Java Break Statement Alternatives: Consider alternatives to break, such as boolean flags or exceptions.

    boolean exitLoop = false;
    while (!exitLoop) {
        // Loop body
        if (someCondition) {
            exitLoop = true;
        }
    }
    
  17. Java Break Statement in Do-While Loop: break can be used in a do-while loop.

    int i = 0;
    do {
        if (i == 5) {
            break;
        }
        // Loop body
        i++;
    } while (i < 10);
    
  18. Common Mistakes with the Break Statement in Java: Be cautious about common mistakes, such as using break outside of loops.

    if (someCondition) {
        break; // Compilation error
    }