BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

What is break Keyword in Java

Last Updated: October 30, 2022 by Chaitanya Singh | Filed Under: java

The break keyword is used inside loops (for, while or do-while) or switch-case block.

When used in loops: It ends the loop as soon as it is encountered, thus it is always accompanied by a condition.

When used in switch block: It prevents execution of the next case statement after the execution of previous case statement.

break in for loop

class JavaExample {
  public static void main(String args[]) {
    for (int i = 1; i <= 20; i++) {
      if (i == 3)
        break; //if i==3 then end the loop
      System.out.println(i);
    }
  }
}

Output:

1
2

break in while loop

class JavaExample {
  public static void main(String args[]) {
    int i = 1;
    while(i<=20){
      if(i == 3)
        break;
      System.out.println(i);
      i++;
    }
  }
}

Output:

1
2

break in do-while

class JavaExample {
  public static void main(String args[]) {
    int i = 1;
    //do-while loop
    do{
      if(i == 3)
        break;
      System.out.println(i);
      i++;
    }while(i<=20);
  }
}

Output:

1
2

break in switch-case block

class JavaExample {
  public static void main(String args[]) {
    int i = 1;
    //switch-case
    switch(i){
      case 1: System.out.println("i value is 1");
      break; //end the switch after case 1 is executed
      case 2: System.out.println("i value is 2");
      break; //end the switch after case 2 is executed
      default: System.out.println("i value is not 1 or 2");
    }
  }
}

Output:

i value is 1

Related guides:

  • Java break statement Tutorial
  • Java switch-case Tutorial
  • Java for loop Tutorial
  • Java while loop Tutorial
  • Java do-while loop Tutorial
❮ Java Keywords

Top Related Articles:

  1. If, If..else Statement in Java with Examples
  2. Continue Statement in Java with example
  3. Java Integer byteValue() Method
  4. What is byte Keyword in Java
  5. Static and dynamic binding in java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap