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
2
3
4
5
6
7
8
9
10
11
12
13
for (int count = 1; count <= 100; count++) {
 
    System.out.println("count = " + count);
 
    if (count > 50) {
 
        break;
 
    }
 
}
 
 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int count = 1;
 
while (true) {
 
    System.out.println("count = " + count);
 
    count++;
 
    if (count > 50) {
 
        break;
 
    }
}
 
 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
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

Karibasappa G C (KB)
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