Should developers avoid using continue in C# or its equivalent in other languages to force the next iteration of a loop? Would arguments for or against overlap with arguments about Goto?
If you use continue then it means your loop elements are not restricted enough, so there is the potential you are looping through unnecessary elements. It also means that at any point inside a loop, you break the 'rules' of the loop. So any change at a later date may break things if you do not notice a continue.
Using continue in Python, personally, is fine. If you're putting it in a if/else statement type condition in python, continue will work.
Every control structure can be implemented in terms of goto but this observation is utterly trivial and useless. goto isn't considered harmful because of its positive effects but because of its negative consequences and these have been eliminated by structured programming.
The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.
I think there should be more use of continue!
Too often I come across code like:
for (...) { if (!cond1) { if (!cond2) { ... highly indented lines ... } } }
instead of
for (...) { if (cond1 || cond2) { continue; } ... }
Use it to make the code more readable!
Is continue
any more harmful than, say, break
?
If anything, in the majority of cases where I encounter/use it, I find it makes code clearer and less spaghetti-like.
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