Java Break Keyword

Java Break Keyword


Break Keyword In Java
  • Break keyword helps us to exit the body of a loop.
  • break quits the loop without executing the rest of the statements in the loop.
  • we use it for termination of all the iterative bodies in the program, which is also known as the Unlabeled break statement
  • we use it for termination of a single iterative body inside another iterative body, also known as Labelled break.
Example  - 
Output - 
Example 2 - 
Output-  Output will be same as previous example 
Example 3 - 
package javaLearnings;
import java.util.Scanner;

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

/*  When used inside a set of nested loops, the break statement will only break out of the innermost loop  */
      for(int i=0; i<3; i++) {
          System.out.print("Pass " + i + ": ");
          for(int j=0; j<100; j++) {
              if(j == 10) break; // terminate loop if j is 10
              System.out.print(j + " ");
          }
          System.out.println();
      }
      System.out.println("Loops complete.");


          }
      }
Output -