break keyword in Java

The break keyword stops the execution of for loop, while loop and switch-case.

In switch case,The execution goes to next statement after all switch cases if any switch case is broken.

The following example shows a break statement inside a for loop:

  1. for (int count = 1; count <= 100; count++) {
  2.  
  3.     System.out.println("count = " + count);
  4.  
  5.     if (count > 50) {
  6.  
  7.         break;
  8.  
  9.     }
  10.  
  11. }
  12.  
  13.  System.out.println("Done");
for (int count = 1; count <= 100; count++) {

    System.out.println("count = " + count);

    if (count > 50) {

        break;

    }

}

 System.out.println("Done");

The above for loop prints values of count from 1 to 50 as it breaks the for loop when count reaches 51 and completely come out of the for loop and proceeds with further execution which prints “Done”

The following example shows break statement is used in a while loop:

  1. int count = 1;
  2.  
  3. while (true) {
  4.  
  5.     System.out.println("count = " + count);
  6.  
  7.     count++;
  8.  
  9.     if (count > 50) {
  10.  
  11.         break;
  12.  
  13.     }
  14. }
  15.  
  16.  System.out.println("Done");
int count = 1;

while (true) {

    System.out.println("count = " + count);

    count++;

    if (count > 50) {

        break;

    }
}

 System.out.println("Done");

The above while loop will stop when the value of count is greater than 50.

The following example shows break statement is used in every case of the switch statement:

  1. int day = 3;
  2.  
  3. String dayOfWeek = "";
  4.  
  5. switch(day) {
  6.  
  7.     case 1:
  8.         dayOfWeek = "Sunday";
  9.         break;
  10.  
  11.     case 2:
  12.         dayOfWeek = "Monday";
  13.         break;
  14.  
  15.     case 3:
  16.         dayOfWeek = "Tuesday";
  17.         break;
  18.  
  19.     case 4:
  20.         dayOfWeek = "Wednesday";
  21.         break;
  22.  
  23.     case 5:
  24.         dayOfWeek = "Thursday";
  25.         break;
  26.  
  27.     case 6:
  28.         dayOfWeek = "Friday";
  29.         break;
  30.  
  31.     case 7:
  32.         dayOfWeek = "Saturday";
  33.         break;
  34.  
  35. }
  36.  
  37. System.out.println("The day of a week is " + dayOfWeek);
int day = 3;

String dayOfWeek = "";

switch(day) {

    case 1:
        dayOfWeek = "Sunday";
        break;

    case 2:
        dayOfWeek = "Monday";
        break;

    case 3:
        dayOfWeek = "Tuesday";
        break;

    case 4:
        dayOfWeek = "Wednesday";
        break;

    case 5:
        dayOfWeek = "Thursday";
        break;

    case 6:
        dayOfWeek = "Friday";
        break;

    case 7:
        dayOfWeek = "Saturday";
        break;

}

System.out.println("The day of a week is " + dayOfWeek);



The break statement prevent the execution of further switch cases and exit from entire switch block.

Note:
When we have nested for loops then using break in innermost loop makes only inner most for loop to break and outer for loop works as expected through each iteration.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on