Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why should we minimize the use of break and continue in loops? [closed]

Tags:

c++

c

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?

like image 792
xyxy Avatar asked Sep 03 '13 09:09

xyxy


People also ask

Why we use break and continue statement in looping system?

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.

What is the purpose of break in loops explain with the help of example?

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.

What is the limitation with break and continue statement in C?

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.

Is using break and continue bad?

Using break as well as continue in a for loop is perfectly fine. It simplifies the code and improves its readability.


2 Answers

Some people think that it's bad to have a too complex control flow, which means things like break, continue and multiple returns. 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.

like image 174
Kerrek SB Avatar answered Nov 16 '22 00:11

Kerrek SB


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".

like image 41
verbose Avatar answered Nov 15 '22 23:11

verbose