VOOZH about

URL: https://www.geeksforgeeks.org/java/usage-of-break-keyword-in-java/

⇱ Usage of Break keyword in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Usage of Break keyword in Java

Last Updated : 22 Mar, 2023

Break keyword is often used inside loops control structures and switch statements. It is used to terminate loops and switch statements in java. When the break keyword is encountered within a loop, the loop is immediately terminated and the program control goes to the next statement following the loop. When the break keyword is used in a nested loop, only the inner loop will get terminated. Even it is used with if statement to terminated when a certain condition is met.

The break keyword has special usage inside the switch statement. Every case in the switch statement is followed by a break keyword, such that whenever the program control jumps to the next case, it wouldn't execute subsequent cases. 

Real-life Illustration:

👁 Image

Consider a person climbing stairs. Now the red line here is depicting the stair on which his/her shoe slips and he/she rolls backs to the base. Remember if he/she slips, he/she will always be landing on the base.

Here in computers in programming language there is a keyword called 'break' which will abruptly stop the further execution of all the statements following it defined in that scope.

Syntax:

break keyword along with a semicolon

break;

Flow chart of Break statement

👁 Image

Use of Break Keyword is mostly found in loop concepts:

Types of loops Usage of 'break' keyword
Simple loopWhile the goal of insertion, delete, or updation is completed and there itself wants the program to terminate
Nested loopWhen the user wants to take command from innermost loop to outer loop 
Infinite loopWhen the program enters into an infinite looping state 

Case 1: Break keyword inside FOR loop

In order to show how to use the break keyword within For loop. Considering when the value of 'i' becomes 39, the break keyword plays its role and terminate the for a loop. All values of 'i' before 39, are displayed in the result.


Output
3 6 9 12 15 18 21 24 27 30 33 36 

Case 2: Break keyword inside WHILE loop

In order to show how to use the break keyword within the While loop. Considering when the value of 'i' becomes 15, the break keyword plays its role and terminate the for a loop. All values of 'i' greater than 15 till 35, are displayed in the result.


Output
35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 

Case 3: Break keyword inside DO-WHILE loop

In order to show how to use the break keyword within the Do-while loop. Considering when the value of 'i' becomes 80, the break keyword plays its role and terminate the for a loop. All values of i'' before 80, are displayed in the result.


Output
0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 

Case 4: Break keyword inside the Switch statement

In order to show how to use the break keyword within For loop. The break keyword is used at the bottom of every switch case to terminate each statement sequence and prevent the mixing of switch-case statements.


Output
TWENTY

Case 5: Break keyword inside label block

In order to show how to use the break keyword within label block. The break keyword is used in the line from where we need to break the label block using break word followed by label name.


Output
Block begin
Comment
Article Tags:
Article Tags: