The break keyword is used to break the control flow from switch & loops.
We can use break keyword in following places.
- Inside switch ( ) to stop fall through.
example:
int x=0
switch(x)
{
case 0 :
System.out.println(0);
case 1 :
System.out.println(1);
break;
case 2 :
System.out.println(2);
default : System.out.println(default);
}
Output is : 0
1
- Inside loop to break loop execution based on some condition.
example:-