<p>Should developers avoid using <a href="http://msdn.microsoft.com/en-us/library/923ahwt1.aspx" rel="nofollow noreferrer">continue</a> 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 <a href="https://stackoverflow.com/questions/46586/goto-still-considered-harmful">Goto</a>? </p>

[{'answer_id': 57606, 'author': 'EBGreen', 'author_id': 1358, 'author_profile': 'https://Stackoverflow.com/users/1358', 'pm_score': 2, 'selected': False, 'text': "<p>I don't think continue could ever be as difficult as goto since continue never moves execution out of the code block that it is in.</p>\n"}, {'answer_id': 57611, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 7, 'selected': True, 'text': '<p>I think there should be more use of continue!</p>\n\n<p>Too often I come across code like:</p>\n\n<pre><code>for (...)\n{\n   if (!cond1)\n   {\n      if (!cond2)\n      {\n          ... highly indented lines ...\n      }\n   }\n}\n</code></pre>\n\n<p>instead of </p>\n\n<pre><code>for (...)\n{\n   if (cond1 || cond2)\n   {\n      continue;\n   }\n\n   ...\n}\n</code></pre>\n\n<p>Use it to make the code more readable!</p>\n'}, {'answer_id': 57616, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 5, 'selected': False, 'text': '<p>Is <strong><code>continue</code></strong> any more harmful than, say, <strong><code>break</code></strong>?</p>\n\n<p>If anything, in the majority of cases where I encounter/use it, I find it makes code clearer and less spaghetti-like.</p>\n'}, {'answer_id': 57619, 'author': 'Patman', 'author_id': 5729, 'author_profile': 'https://Stackoverflow.com/users/5729', 'pm_score': 4, 'selected': False, 'text': "<p>You can write good code with or without continue and you can write bad code with or without continue.</p>\n\n<p>There probably is some overlap with arguments about goto, but as far as I'm concerned the use of continue is equivalent to using break statements (in loops) or return statement from anywhere in a method body - if used correctly it can simplify the code (less likely to contain bugs, easier to maintain).</p>\n"}, {'answer_id': 57632, 'author': 'stukelly', 'author_id': 5891, 'author_profile': 'https://Stackoverflow.com/users/5891', 'pm_score': 0, 'selected': False, 'text': '<p>Continue is a really useful function in most languages, because it allows blocks of code to be skipped for certain conditions.</p>\n\n<p>One alternative would be to uses boolean variables in if statements, but these would need to be reset after every use.</p>\n'}, {'answer_id': 57657, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': -1, 'selected': False, 'text': "<p><code>continue</code> feels wrong to me. <code>break</code> gets you out of there, but <code>continue</code> seems just to be spaghetti.</p>\n\n<p>On the other hand, you can emulate <code>continue</code> with <code>break</code> (at least in Java).</p>\n\n<pre><code>for (String str : strs) contLp: {\n    ...\n       break contLp;\n    ...\n}\n</code></pre>\n\n<p>(This posting had an obvious bug in the above code for over a decade. That doesn't look good for <code>break</code>/<code>continue</code>.)</p>\n\n<p><code>continue</code> can be useful in some circumstances, but it still feels dirty to me. It might be time to introduce a new method.</p>\n\n<pre><code>for (char c : cs) {\n    final int i;\n    if ('0' &lt;= c &amp;&amp; c &lt;= '9') {\n        i = c - '0';\n    } else if ('a' &lt;= c &amp;&amp; c &lt;= 'z') {\n        i = c - 'a' + 10;\n    } else {\n        continue;\n    }\n    ... use i ...\n}\n</code></pre>\n\n<p>These uses should be very rare.</p>\n"}, {'answer_id': 57668, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 1, 'selected': False, 'text': '<p>goto can be used as a continue, but not the reverse.</p>\n\n<p>You can "goto" anywhere, thus break flow control arbitrarily.</p>\n\n<p>Thus continue, not nearly as harmful.</p>\n'}, {'answer_id': 57681, 'author': 'Torlack', 'author_id': 5243, 'author_profile': 'https://Stackoverflow.com/users/5243', 'pm_score': 3, 'selected': False, 'text': '<p>If continue is causing a problem with readability, then chances are you have other problems.  For example, massive amounts of code inside a for loop.  If you have to write large for loops, I would try to stick to using continue close to the top of the for loop.  Otherwise, a continue buried deep in the middle of a for loop can easily be missed.</p>\n'}, {'answer_id': 57765, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 3, 'selected': False, 'text': "<p>There are not harmful keywords. There's only harmful uses of them.</p>\n\n<p>Goto is not harmful per se, neither is continue. They need to be used carefully, that's all.</p>\n"}, {'answer_id': 57786, 'author': 'Jack Bolding', 'author_id': 5882, 'author_profile': 'https://Stackoverflow.com/users/5882', 'pm_score': 3, 'selected': False, 'text': '<p>I like to use continue at the beginning of loops for handling simple if conditions.</p>\n\n<p>To me it makes the code more readable since there is not extra nesting and you can see that I have explicitly dealt with these cases.</p>\n\n<p>Is this the same reason that I would use a goto?  Perhaps.  I do use them for readability at times and to stop the nesting of code but I usually use them more for cleanup/error handling. </p>\n'}, {'answer_id': 57806, 'author': 'Nij', 'author_id': 6004, 'author_profile': 'https://Stackoverflow.com/users/6004', 'pm_score': 1, 'selected': False, 'text': "<p>Others have hinted at it... but continue and break are enforced by the <strong>compiler</strong> and have their own associated rules. Goto has no such limitations, though the net effect <em>might</em> almost be the same, in some circumstances.</p>\n\n<p>I do not consider continue or break to be harmful per se, though I'm sure either can be used poorly in a way that would make any sane programmer gag.</p>\n"}, {'answer_id': 58320, 'author': 'Christopher Elliott', 'author_id': 5072, 'author_profile': 'https://Stackoverflow.com/users/5072', 'pm_score': -1, 'selected': False, 'text': "<p>I believe the bottom line argument against continue is that it makes it harder to PROVE that the code is correct. This is prove in the mathematical sense. But it probably doesn't matter to you because no one has the resources to 'prove' a computer program that is significantly complex. </p>\n\n<p>Enter the static-analysis tools. You may make things harder on them...</p>\n\n<p>And the goto, that sounds like a nightmare for the same reasons but at any random place in code. </p>\n"}, {'answer_id': 58923, 'author': 'Peter Bernier', 'author_id': 6112, 'author_profile': 'https://Stackoverflow.com/users/6112', 'pm_score': 0, 'selected': False, 'text': "<p>I'd say yes. To me, it just breaks the 'flow' of a fluidly-written piece of code.</p>\n\n<p>Another argument could also be that if you stick to the basic keywords supported by most modern languages, then your program flow (if not the logic or code) could be ported to any other language. Having an unsupported keyword (ie, continue or goto) would break that.</p>\n\n<p>It's really more of a personal preference, but I've never had to use it and don't really consider it an option when I'm writing new code. (same as goto.)</p>\n"}, {'answer_id': 63187, 'author': 'Ta01', 'author_id': 7280, 'author_profile': 'https://Stackoverflow.com/users/7280', 'pm_score': 2, 'selected': False, 'text': "<p>If you are iterating through any kind of a result set, and performing operations on said results, for e.g within a for each, and if one particular result caused a problem, its rather useful in capturing an expected error (via try-catch), logging it, and moving on to the next result via continue. Continue is especially useful, imo, for unattended services that do jobs at odd hours, and one exception shouldn't affect the other x number of records.</p>\n"}, {'answer_id': 436952, 'author': 'blabla999', 'author_id': 48469, 'author_profile': 'https://Stackoverflow.com/users/48469', 'pm_score': 2, 'selected': False, 'text': '<p>I\'d say: "it depends".</p>\n\n<p>If you have reasonably small loop code (where you can see the whole loop-code without scrolling) its usually ok to use a continue.</p>\n\n<p>However, if the loops body is large (for example due to a big switch), and there is some followup code (say below the switch), you may easily introduce bugs by adding a continue and thus skipping over that code sometimes. I have encountered this in the heart of a bytecode interpreter, where some instrumentation code was sometimes not executed due to a continue in some case-branches.</p>\n\n<p>This might be a somewhat artificially constructed case, but I generally try to avoid continue and use an if (but not nesting too deep as in the Rob\'s sample code).</p>\n'}, {'answer_id': 436963, 'author': 'too much php', 'author_id': 28835, 'author_profile': 'https://Stackoverflow.com/users/28835', 'pm_score': 2, 'selected': False, 'text': '<p>As far as this programmer is concerned, <strong>Nested if/else</strong> considered harmful.</p>\n'}, {'answer_id': 619943, 'author': 'Victor Rodrigues', 'author_id': 21668, 'author_profile': 'https://Stackoverflow.com/users/21668', 'pm_score': 2, 'selected': False, 'text': '<ol>\n<li><p>Using continue at the beginning of a loop to avoid iteration over unnecessary elements is not harmful and can be very useful, but using it in the middle of nested ifs and elses can turn the loop code into a complex maze, to understand and validate.</p></li>\n<li><p>I think its usage avoidance is also the result of a semantic misunderstanding. People who does never see/write \'continue\' keyword on their code, when seeing a code with continue can interpret it as "the continuation of the natural flow". If instead of continue we had <strong>next</strong>, for instance, I think more people would appreciate this valuable cursor feature.</p></li>\n</ol>\n'}]
['https://Stackoverflow.com/questions/57600', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2831/']