When I was a freshman, our instructor allowed us to use break
or continue
in loops. I did it most of the time back then since it terminates/continues the loop. And now I'm in sophomore years, my instructor told me that the use of break
/continue
is not advisable. Can you tell me why? What affects break/continue by the way?
The one-token statements continue and break may be used within loops to alter control flow; continue causes the next iteration of the loop to run immediately, whereas break terminates the loop and causes execution to resume after the loop. Both control structures must appear in loops.
The purpose the break statement is to break out of a loop early. For example if the following code asks a use input a integer number x. If x is divisible by 5, the break statement is executed and this causes the exit from the loop.
The Break statement is used to exit from the loop constructs. The continue statement is not used to exit from the loop constructs. The break statement is usually used with the switch statement, and it can also use it within the while loop, do-while loop, or the for-loop.
Using break as well as continue in a for loop is perfectly fine. It simplifies the code and improves its readability.
Some people think that it's bad to have a too complex control flow, which means things like break
, continue
and multiple return
s. The reason is not technical, but mostly that complex control flow can make it harder to verify, test and and reason about a program.
It is however largely a matter of style, personal taste, and your overall structure. With small, well-purposed functions, there might be little to no harm in having multiple possible flows. In C++ in particular, early exit is a popular idiom and can often make code easier to follow.
At least in C, you should not be using break
and/or continue
"most of the time" (as your question says you used to do) to control the flow of your loops. Your loop condition should indicate under what circumstances the loop should stop; somebody maintaining your code should not have to dig through the code in the body of your loop to see what triggers the break
that causes the loop to stop.
For example, let's say you want to read a number of integers from a file inputFile
to see if one of the integers is 500. One way of structuring the loop is:
while (fgets (buffer, sizeof (buffer), inputFile)){
sscanf (buffer, "%d", &num);
if (num == 500)
break;
}
Here, the person reading your code has to read your entire while
loop to figure out what you are actually looking for in the file. If you write this without the break
:
while ((num != 500) && fgets (buffer, sizeof (buffer), inputFile))
sscanf (buffer, "%d", &num);
the loop condition itself tells the reader exactly what your code is trying to do, which makes it a lot more easy to understand quickly. Also, as a bonus, you have saved a few lines of code.
Now imagine a more complicated while
or for
loop, where the break
is buried deep inside the body of the loop. It's easy to see why trying to find the break
trigger would get annoying. Having a properly structured loop condition is much more, um, self-documenting.
There are, of course, cases where break
and continue
are in fact good ways to write the code. For example, if the condition at which the loop should end might occur in the middle of the loop execution, and there's a long set of statements that follow inside the loop, and executing those statements would add processing time without accomplishing anything useful, sure, go ahead and use the break
. But those cases are the exception, not the "most of the time".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With