Why is a bad idea going on in the loop?

Douglas Crockford says it's usually best to reorganize continue inside a loop.

Why does the loop continue to be considered bad?

+7
loops continue
Feb 06 2018-11-11T00:
source share
3 answers

Using continue means you don't have enough conditions written to while .

Instead, you should use if inside the while or add a condition to the while .

+7
Feb 06 '11 at 14:52
source share

Using goto, break, continue, throw, or return inside the loop body can also have an undesirable effect. Here is another example where loop control and loop body are tightly intertwined. Does he write 1, 2, and 3 as before? Are you sure?

 int value = 1; for (;;++value) { cout << value << endl; if (value != 4) continue; else break; } 

Perhaps you think that we advise you not to use return expressions inside the bodies of the cycle more diligently. Do I really mean that? Yes. Functions that return something must do this with a single return statement at the very end of the function. Here are a few practical reasons:

Link

Disclaimer: not my material, I refer to the source

+4
Feb 06 2018-11-12T00:
source share

The continue effect is somehow comparable to a goto at the beginning of the loop. Therefore, it makes your code more difficult to understand - for example, goto s.

+1
Feb 06 '11 at
source share



All Articles