Java Break Statement

Break Statement ?

Break Statement is a loop control statement which is used to terminate the loop.
The break statement is used in two situations:
1) When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
2) It can be used to terminate a case in the switch statement (covered in the next chapter).


Break Statement :

In real practice, break statement is almost always used inside the body of conditional statement (if...else) inside the loop.

Basically break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop based on some condition.


Example 1:



Output :

10
20

Example 2:



Output :

Value of variable is: 0
Value of variable is: 1
Value of variable is: 2
Out of while-loop