break and continue in Java are two essential keyword beginners needs to familiar while using loops ( for loop, while loop and do while loop). break statement in java is used to break the loop and transfers control to the line immediate outside of loop while continue is used to escape current execution and transfers control back to start of the loop. Both break and continue allows programmer to create sophisticated algorithm and looping constructs. In this java tutorial we will see example of break and continue statement in Java and some important points related to breaking the loop using label and break statement. break keyword can also be used inside switch statement to break current choice and if not used it can cause fall-through on switch. Well break is not alone on breaking switch case you can also sued throw keyword on switch case.
This Java tutorial is next in series of articles in looping like 4 ways to loop Map in Java and How to loop on ArrayList in Java. If you are new here and haven’t gone through those articles then you may find them useful.
break and continue statement in Java - example.
In this example we are calculating sum of odd numbers until number 5 appear, where we break the loop by using break statement. after calculating sum we also call continue which cause last line of loop to not execute. remember call to continue is inside if block which checks for odd numbers, so that line will appear in case of even number but not in case of odd number. This example of break and continue shows that upon calling break, loop terminates and control goes to first line outside the loop while upon calling continue, loop doesn't terminate but rest of execution doesn't happen and loop continues from next iteration.
continue and break examples with label in Java
You can use labels with break and continue. labels are where you can shift control from break and continue. by default break goes outside of loop but if you more than one loop you can use break statement to break a specific loop by providing label. same is true for continue. if you are working on nested loops labels gives more control to break and continue. In following example of break and continue with labels. we have a nested loop and we have created two labels OUTER and INNER. OUTER label is for outer loop and INNER label is for inner loop. we are iterating through array and print value of odd number from outer loop and then use continue statement, which ignores execution of inner loop. if its even number than inner loop executes and breaks as soon as it prints the number.
As shown in above example of break and continue with labels, It provides lot of convenience to break and continue in case of nested loop.
important point about break and continue in Java
1. break keyword transfers control to next statement outside loop while continue keyword transfers control to beginning of loop, ignoring rest of lines in loop.
2. break can also be used inside CASE statement of switch construct.
3. an optional label can be provided after break and continue which cause to apply break and continue on specified loop.
That's all on break and continue statement in Java. break and continue allows programmer to control flow of execution in loops. We have also seen example of break and continue with and without labels , which allows you to write more sophisticated looping construct in java.
Other Java beginners tutorial you may like