Java Break Statement

Java Break Statement 

A break statement is used when you want to get out at a particular condition inside a loop or switch case, when it encounters a break statement the loop immediately terminates and control goes to the next statement.

Syntax:

Statement;
break;

When to use Break Statement :

Suppose you have to print numbers from 1 to 4 but are running a loop 10 times, In this situation using a break statement when i becomes 5 will terminate the loop.

If you have nested for loop then break will only terminate the innermost loop, the outer loop will not be affected.

Code:

Java Code

class Main {
  public static void main(String args[]) {

    for (int i = 1; i <= 10; i++) {
      if (i == 5) {
        break; // terminate loop when i equals 5
      }
      System.out.println(i);
    }
  }
}

Output: 1 2 3 4

Break Statement in For loop:

When the key matches the element in the given array, the index of the element is printed and the loop is terminated using the break statement.

Java Code

class Main {
  public static void main(String args[]) {
    // Your code goes here
    int[] arr = {1,2,3,4,5,6,7,8,9};
     
    int key = 2;

    for (int i = 0; i < arr.length; i++) {
      if (arr[i] == key) {
        System.out.print(i); // prints index of key
        break; // terminates the for loop
      }
    }
  }
}

Output: 1

Break Statement in While loop:

When the key matches the element in the given array, the index of the element is printed and the loop is terminated using the break statement.

Code:

Java Code

class Main {
  public static void main(String args[]) {
    // Your code goes here
    int[] arr = {1,2,3,4,5,6,7,8,9};
      
    int key = 2;
    int i = 0;
    while (i < arr.length) {
      if (arr[i] == key) {
        System.out.print(i); // prints index of key
        break; // terminates the for loop
      }
      i++;
    }
  }
}

Output: 1

Break Statement in Switch case:

When you want a single case to be executed using a break statement, the break will terminate the switch case and no other cases will be getting executed. The default is just a keyword that will output a default value.

Code:

Java Code

class Main {
  public static void main(String args[]) {
    // Your code goes here
    int num = 4;
    switch (num) {
    case 1:
      System.out.println("one");
      break;
    case 2:
      System.out.println("two");
      break;
    case 3:
      System.out.println("three");
      break;
    case 4:
      System.out.println("four");
      break;
    case 5:
      System.out.println("five");
      break;
    case 6:
      System.out.println("six");
      break;
    case 7:
      System.out.println("seven");
      break;
    case 8:
      System.out.println("eight");
      break;
    case 9:
      System.out.println("nine");
      break;
    case 10:
      System.out.println("ten");
      break;
    }
  }
}

Output: four

Special thanks to Rushikesh Aadav for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article