While Java may not have been a commonly used interchangeable term for coffee among caffeine consumers, but it stands alone as the only name that has inspired a computer programming language. Assuming you are on your first coffee break for the day, let’s a brief exploration of break statement in Java language in context of loops.
Unlike the English word “break” which gives a connotation of momentary pause from wear and tear, Java break statement, on the contrary, allows desirable navigation of program control within an iteration when certain conditions are met. Break is one of the Transfer statements in Java besides Continue (which we will look into later), Return, Try-catch-finally, etc. Transfer statements alter the way a logic gets executed and are often used in iteration. In essence, break statement will break out of immediate loop while labeled break enables breaking out of target loop. In this blog, we will take a look at some of the different control flow statements and how java break works in tandem with them.
Switch Statement
Code Explanation: In the switch case statement above, it is expected that when variable z finds a matching case for its value, it will print out 0 and break out of the switch case. While that’s what’s happening on the right hand side of the code (as shown in the console), the left code logic is displaying unexpected flow. We can see that it is printing all numbers 0, 1 and 2. This is because the “break;” statement was not added for each of the cases. Rather it was added just once inside “case 2”. Therefore, the program not only went to case 0 and printed 0 but it kept on going into every case blocks until it hits the break statement inside case 2.
This seeming mishap could be a good design in some other logic. Let’s say, if we are trying to book a reservation and the reservation could be done till December, then the available month could be any from current month and breaking out from month switch once control goes inside case 12.
Regular Break and Labeled Break inside Loop
Code Explanation: Regular break statement breaks out of Immediate Loop. As such, in the left code, program breaks out of inner loop at line 23 when y==2 but re-enters as outer loop is not broken and outer loop iteration has not yet been complete. In the console, it can be seen that outer loop completes its full cycle and reaches value of 2 while inner loop is always broken out of when y reaches 2.
On the right side of the code, labels have been used for outer loop and inner loop blocks of code. On line 19, the program is asked to break out of loop with label “outerloop” which is the outside loop when y reaches value 2 and the control comes at line 25. In this way, the program execution can be directed to break out of any target loop mentioned with its corresponding label.
Break inside Do-while and While loop
Code Explanation: Be it regular or labeled do-while or while loop, break statement will cause the program control to exit the loop when a condition is met. The control will come out to line 26 and 28 respectively.
Continue
While we are at it, let’s continue and take a quick look at Continue statement as well. Linguistically, the first thought that comes to mind is Continue would be logically opposite to Break statement. But that’s not the case. Continue statement will just skip the current iteration and move over to the next iteration without breaking out of the current loop. Let’s illustrate it with some example:
Continue inside For Loop:
Code Explanation: In the left code, when program reaches “continue;” on line 32, it will then skip the rest of the lines of codes in current iteration and will directly jump back to line 30 to the next iteration of i=4. In this process, line 34 was skipped and no “3” was printed in the console.
For labeled continue statement, programmer gets to tell the control to skip current loop altogether without completing its full iteration and jump on to some other outer loop and move on to that outer loop’s next iteration. On the right code, when program is asked to continue to outerloop: label, it abandons its inner loop and jumps to line 35 so it can start from the next iteration of label “outerloop:”. While outloop: completes its full iteration, innerloop: doesn’t.
Continue statement with While and Do-While loop
Code Explanation: In a simple while loop, continue statement will cause the control to move to line 105 where the condition is checked and iteration begins. That’s why, it is not printing 2 as continue statement caused control to completely ignore lines 108-111.
To make things a little more interesting, the following example of continue statement in do-while loop included some pre and post-increment
Code Explanation: The code above shows that the do-while loop started with x initialized at 0. In line 92, x++ is post incremented by 1, that is, value of x will be unchanged at line 92 (which is zero) but will be 1 on line 93. Then on line 95, there is pre-increment of x by 1 and the value of x on that line changes from 1 to 2. Now the condition is checking if 2 is less than 5. Since the condition is true, the control will enter the block and reach continue statement. Continue in do-while or while is little tricky. Since continue statement abandons current iteration and moves to the next one, it may seem that the control will go back to line 92 to start the whole iteration from top. While that’s not the case, the control will instead go to the very bottom of the current iteration to line 100 skipping lines 97-99. There it will again do a pre-increment of x driving the value up to 3 which is less than 10 and satisfying the while clause, and only then the control flow move on to the next iteration to line 92 in do while loop.
In second iteration, the control does not enter the “if” block and therefore reaches lines 98 and 99, completing the whole iteration as long as the while expression becomes false.
Continue and Break – with and without label
Understanding how these transfer statements work would greatly enhance the way logics and flows are written to achieve desired functionality of code blocks. It surely does take bit of practice to get out of confusion as to which one does what. To speed up that process and to end your coffee break with good note, presented below are four blocks of codes with continue and break statements without console output.
Write your expected output of each of these code blocks in the comment below and to check if they are correct, feel free to try them in your editor. I’m sure the learning so far was enough to get them right at first try. Hope you enjoy it!