java2db

How to use labeled break and continue control flow statements in java



The labeled break and continue are special branching statements .These statements have the additional capability to control the nested loops.

 

labeled break and continue Labeled continue statement :

labeled break and continue  It will continues to the next iteration of the (outer) loop .

labeled break and continue  If any statement presents after labeled continue, it will throws unreachable statement exception.

 

labeled break and continue Labeled break statement :

labeled break and continue  It can terminate the execution from the entire nested (both inner and outer) loop at a time.

labeled break and continue  If any statement presents after labeled break, it will throws unreachable statement exception.

Syntax:

ContinueLabelName :
OuterLoop
{
InnerLoop
{
———–//statements;
condition{
———–//statements;
continue ContinueLabelName ;}
}
}
BreakLabelName :
OuterLoop
{
InnerLoop
{
———–//statements;
condition{
———–//statements;
break BreakLabelName ;}
}
}

labeled break and continue Labeled continue example :

 

If the condition is true ,it skip the (current iteration) inner loop and continues the iteration from the outer loop.

labeled break and continue Labeled break example : In the above example ,just replace the continue with break keyword . 

 

If the condition is true then the loop completely terminates form the both inner and outer loops.

labeled break and continue One more example for Labeled break and continue statements :

Execution Result :

labeled break and continueWe can use labeled break , continue statements for complex nested loops .
labeled break and continueThe label should be declare at top of the outer loop , but not in the bottom of the outer loop.
labeled break and continueIf the label declared after the outer loop, it throws illegal start of expression exception.




Related Posts :
What are the main basic control flow statements in java
   [will not be published]



^ <