<p>In a C# (feel free to answer for other languages) loop, what's the difference between <code>break</code> and <code>continue</code> as a means to leave the structure of the loop, and go to the next iteration?</p>
<p>Example:</p>
<pre><code>foreach (DataRow row in myTable.Rows)
{
    if (someConditionEvalsToTrue)
    {
        break; //what's the difference between this and continue ?
        //continue;
    }
}
</code></pre>

[{'answer_id': 6415, 'author': 'palmsey', 'author_id': 521, 'author_profile': 'https://Stackoverflow.com/users/521', 'pm_score': 5, 'selected': False, 'text': '<p><code>break</code> would stop the <code>foreach</code> loop completely, <code>continue</code> would skip to the next <code>DataRow</code>.</p>\n'}, {'answer_id': 6417, 'author': 'Michael Stum', 'author_id': 91, 'author_profile': 'https://Stackoverflow.com/users/91', 'pm_score': 12, 'selected': True, 'text': '<p><code>break</code> will exit the loop completely, <code>continue</code> will just <strong>skip</strong> the current iteration.</p>\n\n<p>For example:</p>\n\n<pre><code>for (int i = 0; i &lt; 10; i++) {\n    if (i == 0) {\n        break;\n    }\n\n    DoSomeThingWith(i);\n}\n</code></pre>\n\n<p>The break will cause the loop to exit on the first iteration - <code>DoSomeThingWith</code> will never be executed. This here:</p>\n\n<pre><code>for (int i = 0; i &lt; 10; i++) {\n    if(i == 0) {\n        continue;\n    }\n\n    DoSomeThingWith(i);\n}\n</code></pre>\n\n<p>Will not execute <code>DoSomeThingWith</code> for <code>i = 0</code>, but the loop will <strong>continue</strong> and <code>DoSomeThingWith</code> will be executed for <code>i = 1</code> to <code>i = 9</code>.</p>\n'}, {'answer_id': 6442, 'author': 'JeremiahClark', 'author_id': 581, 'author_profile': 'https://Stackoverflow.com/users/581', 'pm_score': 9, 'selected': False, 'text': '<p>A really easy way to understand this is to place the word "loop" after each of the keywords.  The terms now make sense if they are just read like everyday phrases.</p>\n\n<p><strong><code>break</code></strong> loop - looping is broken and stops.</p>\n\n<p><strong><code>continue</code></strong> loop - loop continues to execute with the next iteration.</p>\n'}, {'answer_id': 6446, 'author': 'yukondude', 'author_id': 726, 'author_profile': 'https://Stackoverflow.com/users/726', 'pm_score': 4, 'selected': False, 'text': "<p>There are more than a few people who don't like <code>break</code> and <code>continue</code>. The latest complaint I saw about them was in <em>JavaScript: The Good Parts</em> by Douglas Crockford. But I find that sometimes using one of them really simplifies things, especially if your language doesn't include a <code>do-while</code> or <code>do-until</code> style of loop.</p>\n\n<p>I tend to use <code>break</code> in loops that are searching a list for something. Once found, there's no point in continuing, so you might as well quit.</p>\n\n<p>I use <code>continue</code> when doing something with most elements of a list, but still want to skip over a few.</p>\n\n<p>The <code>break</code> statement also comes in handy when polling for a valid response from somebody or something. Instead of:</p>\n\n<pre><code>Ask a question\nWhile the answer is invalid:\n    Ask the question\n</code></pre>\n\n<p>You could eliminate some duplication and use:</p>\n\n<pre><code>While True:\n    Ask a question\n    If the answer is valid:\n        break\n</code></pre>\n\n<p>The <code>do-until</code> loop that I mentioned before is the more elegant solution for that particular problem:</p>\n\n<pre><code>Do:\n    Ask a question\n    Until the answer is valid\n</code></pre>\n\n<p>No duplication, and no <code>break</code> needed either.</p>\n"}, {'answer_id': 6555, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 3, 'selected': False, 'text': '<p>Ruby unfortunately is a bit different. \nPS: My memory is a bit hazy on this so apologies if I\'m wrong</p>\n\n<p>instead of break/continue, it has break/next, which behave the same in terms of loops</p>\n\n<p>Loops (like everything else) are expressions, and "return" the last thing that they did. Most of the time, getting the return value from a loop is pointless, so everyone just does this</p>\n\n<pre><code>a = 5\nwhile a &lt; 10\n    a + 1\nend\n</code></pre>\n\n<p>You can however do this</p>\n\n<pre><code>a = 5\nb = while a &lt; 10\n    a + 1\nend # b is now 10\n</code></pre>\n\n<p>HOWEVER, a lot of ruby code \'emulates\' a loop by using a block.\nThe canonical example is</p>\n\n<pre><code>10.times do |x|\n    puts x\nend\n</code></pre>\n\n<p>As it is much more common for people to want to do things with the result of a block, this is where it gets messy.\nbreak/next mean different things in the context of a block.</p>\n\n<p>break will jump out of the code that called the block</p>\n\n<p>next will skip the rest of the code in the block, and \'return\' what you specify to the caller of the block. This doesn\'t make any sense without examples.</p>\n\n<pre><code>def timesten\n    10.times{ |t| puts yield t }\nend\n\n\ntimesten do |x|\n   x * 2\nend\n# will print\n2\n4\n6\n8 ... and so on\n\n\ntimesten do |x|\n    break\n    x * 2\nend\n# won\'t print anything. The break jumps out of the timesten function entirely, and the call to `puts` inside it gets skipped\n\ntimesten do |x|\n    break 5\n    x * 2\nend\n# This is the same as above. it\'s "returning" 5, but nobody is catching it. If you did a = timesten... then a would get assigned to 5\n\ntimesten do |x|\n    next 5\n    x * 2\nend \n# this would print\n5\n5\n5 ... and so on, because \'next 5\' skips the \'x * 2\' and \'returns\' 5.\n</code></pre>\n\n<p>So yeah. Ruby is awesome, but it has some awful corner-cases. This is the second worst one I\'ve seen in my years of using it :-)</p>\n'}, {'answer_id': 16866, 'author': 'SemiColon', 'author_id': 1994, 'author_profile': 'https://Stackoverflow.com/users/1994', 'pm_score': 7, 'selected': False, 'text': '<p><strong>break</strong> causes the program counter to jump out of the scope of the innermost loop</p>\n\n<pre><code>for(i = 0; i &lt; 10; i++)\n{\n    if(i == 2)\n        break;\n}\n</code></pre>\n\n<p>Works like this</p>\n\n<pre><code>for(i = 0; i &lt; 10; i++)\n{\n    if(i == 2)\n        goto BREAK;\n}\nBREAK:;\n</code></pre>\n\n<p><strong>continue</strong> jumps to the end of the loop.  In a for loop, continue jumps to the increment expression.</p>\n\n<pre><code>for(i = 0; i &lt; 10; i++)\n{\n    if(i == 2)\n        continue;\n\n    printf("%d", i);\n}\n</code></pre>\n\n<p>Works like this</p>\n\n<pre><code>for(i = 0; i &lt; 10; i++)\n{\n    if(i == 2)\n        goto CONTINUE;\n\n    printf("%d", i);\n\n    CONTINUE:;\n}\n</code></pre>\n'}, {'answer_id': 224965, 'author': 'Maltrap', 'author_id': 10644, 'author_profile': 'https://Stackoverflow.com/users/10644', 'pm_score': 3, 'selected': False, 'text': '<p>Simple answer: </p>\n\n<p><strong>Break</strong> exits the loop immediately.<br>\n<strong>Continue</strong> starts processing the next item. (If there are any, by jumping to the evaluating line of the for/while)</p>\n'}, {'answer_id': 674617, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Please let me state the obvious: note that adding neither break nor continue, will resume your program; i.e. I trapped for a certain error, then after logging it, I wanted to resume processing, and there were more code tasks in between the next row, so I just let it fall through.</p>\n'}, {'answer_id': 4497072, 'author': 'Pritom Nandy', 'author_id': 548420, 'author_profile': 'https://Stackoverflow.com/users/548420', 'pm_score': 4, 'selected': False, 'text': '<p>All have given a very good explanation. I am still posting my answer just to give an example if that can help.</p>\n\n<pre><code>// break statement\nfor (int i = 0; i &lt; 5; i++) {\n    if (i == 3) {\n        break; // It will force to come out from the loop\n    }\n\n    lblDisplay.Text = lblDisplay.Text + i + "[Printed] ";\n}\n</code></pre>\n\n<p>Here is the output:</p>\n\n<blockquote>\n  <p>0[Printed] 1[Printed] 2[Printed] </p>\n</blockquote>\n\n<p>So 3[Printed] &amp; 4[Printed] will not be displayed as there is break when i == 3</p>\n\n<pre><code>//continue statement\nfor (int i = 0; i &lt; 5; i++) {\n    if (i == 3) {\n        continue; // It will take the control to start point of loop\n    }\n\n    lblDisplay.Text = lblDisplay.Text + i + "[Printed] ";\n}\n</code></pre>\n\n<p>Here is the output:</p>\n\n<blockquote>\n  <p>0[Printed] 1[Printed] 2[Printed] 4[Printed] </p>\n</blockquote>\n\n<p>So 3[Printed]  will not be displayed as there is continue when i == 3</p>\n'}, {'answer_id': 15310861, 'author': 'Gopesh Sharma', 'author_id': 1266135, 'author_profile': 'https://Stackoverflow.com/users/1266135', 'pm_score': 2, 'selected': False, 'text': '<p>To break completely out of a foreach loop, <strong>break</strong> is used;</p>\n\n<p>To go to the next iteration in the loop, <strong>continue</strong> is used;</p>\n\n<p><strong>Break</strong> is useful if you’re looping through a collection of Objects (like Rows in a Datatable) and you are searching for a particular match, when you find that match, there’s no need to continue through the remaining rows, so you want to break out.</p>\n\n<p><strong>Continue</strong> is useful when you have accomplished what you need to in side a loop iteration. You’ll normally have <strong>continue</strong> after an <strong>if</strong>.</p>\n'}, {'answer_id': 15675992, 'author': 'Sona Rijesh', 'author_id': 1522741, 'author_profile': 'https://Stackoverflow.com/users/1522741', 'pm_score': 3, 'selected': False, 'text': '<p><strong>Break</strong></p>\n\n<p>Break forces a loop to exit immediately. </p>\n\n<p><strong>Continue</strong></p>\n\n<p>This does the opposite of break. Instead of terminating the loop, it immediately loops again, skipping the rest of the code.</p>\n'}, {'answer_id': 19300205, 'author': 'Colonel Panic', 'author_id': 284795, 'author_profile': 'https://Stackoverflow.com/users/284795', 'pm_score': 3, 'selected': False, 'text': '<p>By example</p>\n\n<pre><code>foreach(var i in Enumerable.Range(1,3))\n{\n    Console.WriteLine(i);\n}\n</code></pre>\n\n<p>Prints 1, 2, 3 (on separate lines).</p>\n\n<p>Add a break condition at i = 2</p>\n\n<pre><code>foreach(var i in Enumerable.Range(1,3))\n{\n    if (i == 2)\n        break;\n\n    Console.WriteLine(i);\n}\n</code></pre>\n\n<p>Now the loop prints 1 and stops.</p>\n\n<p>Replace the break with a continue.</p>\n\n<pre><code>foreach(var i in Enumerable.Range(1,3))\n{\n    if (i == 2)\n        continue;\n\n    Console.WriteLine(i);\n}\n</code></pre>\n\n<p>Now to loop prints 1 and 3 (skipping 2).</p>\n\n<p>Thus, <code>break</code> stops the loop, whereas <code>continue</code> skips to the next iteration.</p>\n'}, {'answer_id': 35169830, 'author': 'Umair Khalid', 'author_id': 4732930, 'author_profile': 'https://Stackoverflow.com/users/4732930', 'pm_score': 2, 'selected': False, 'text': '<p>if you don\'t want to use <strong>break</strong> you just increase value of I in such a way that it make iteration condition false and loop will not execute on next iteration.</p>\n\n<pre><code>for(int i = 0; i &lt; list.Count; i++){\n   if(i == 5)\n    i = list.Count;  //it will make "i&lt;list.Count" false and loop will exit\n}\n</code></pre>\n'}, {'answer_id': 47664169, 'author': 'BenKoshy', 'author_id': 4880924, 'author_profile': 'https://Stackoverflow.com/users/4880924', 'pm_score': 6, 'selected': False, 'text': '<h2>When to use break vs continue?</h2>\n<ol>\n<li><strong>Break</strong> - We\'re leaving the loop forever. Good bye.</li>\n</ol>\n<p><a href="https://i.stack.imgur.com/s97tT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/s97tT.png" alt="Break" /></a></p>\n<ol start="2">\n<li><strong>Continue</strong> - means that you\'re gonna give today a rest and sort it all out tomorrow (i.e. skip the current iteration)!</li>\n</ol>\n<p><a href="https://i.stack.imgur.com/MilvY.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MilvY.jpg" alt="Continue" /></a></p>\n<p>(Corny stories ¯\\<em>(ツ)</em>/¯  but hopefully helps you remember)</p>\n'}, {'answer_id': 48887451, 'author': 'dba', 'author_id': 2408978, 'author_profile': 'https://Stackoverflow.com/users/2408978', 'pm_score': 0, 'selected': False, 'text': '<p>As for other languages:</p>\n<pre class="lang-vb prettyprint-override"><code>    \'VB\n    For i=0 To 10\n       If i=5 then Exit For \'= break in C#;\n       \'Do Something for i&lt;5\n    next\n     \n    For i=0 To 10\n       If i=5 then Continue For \'= continue in C#\n       \'Do Something for i&lt;&gt;5...\n    Next\n</code></pre>\n'}, {'answer_id': 69146895, 'author': 'Mostafa Ghorbani', 'author_id': 12094348, 'author_profile': 'https://Stackoverflow.com/users/12094348', 'pm_score': 0, 'selected': False, 'text': "<p>Since the example written here are pretty simple for understanding the concept I think it's also a good idea to look at the more <strong>practical version</strong> of the <strong>continue statement</strong> being used.\nFor example:</p>\n<p>we ask the user to enter 5 unique numbers if the number is already entered we give them an error and we continue our program.</p>\n<pre><code>static void Main(string[] args)\n        {\n            var numbers = new List&lt;int&gt;();\n\n\n            while (numbers.Count &lt; 5)\n            { \n            \n                Console.WriteLine(&quot;Enter 5 uniqe numbers:&quot;);\n                var number = Convert.ToInt32(Console.ReadLine());\n\n\n\n                if (numbers.Contains(number))\n                {\n                    Console.WriteLine(&quot;You have already entered&quot; + number);\n                    continue;\n                }\n\n\n\n                numbers.Add(number);\n            }\n\n\n            numbers.Sort();\n\n\n            foreach(var number in numbers)\n            {\n                Console.WriteLine(number);\n            }\n\n\n        }\n</code></pre>\n<p>lets say the users input were 1,2,2,2,3,4,5.the result printed would be:</p>\n<pre><code>1,2,3,4,5\n</code></pre>\n<p>Why? because every time user entered a number that was already on the list, our program ignored it and didn't add what's already on the list to it.\nNow if we try the same code but <strong>without continue statement</strong> and let's say with the same input from the user which was 1,2,2,2,3,4,5.\nthe output would be :</p>\n<pre><code>1,2,2,2,3,4\n</code></pre>\n<p>Why? because there was no continue statement to let our program know it should ignore the already entered number.</p>\n<p>Now for the <strong>Break statement</strong>, again I think its the best to show by example. For example:</p>\n<p>Here we want our program to continuously ask the user to enter a number. We want the loop to terminate when the user types “ok&quot; and at the end Calculate the sum of all the previously entered numbers and display it on the console.</p>\n<p>This is how the <strong>break statement</strong> is used in this example:</p>\n<pre><code>{\n            var sum = 0;\n            while (true)\n            {\n                Console.Write(&quot;Enter a number (or 'ok' to exit): &quot;);\n                var input = Console.ReadLine();\n\n                if (input.ToLower() == &quot;ok&quot;)\n                    break;\n\n                sum += Convert.ToInt32(input);\n            }\n            Console.WriteLine(&quot;Sum of all numbers is: &quot; + sum);\n        }\n</code></pre>\n<p>The program will ask the user to enter a number till the user types &quot;OK&quot; and only after that, the result would be shown. Why?\nbecause break statement finished or stops the ongoing process when it has reached the condition needed.</p>\n<p>if there was no break statement there, the program would keep running and nothing would happen when the user typed &quot;ok&quot;.</p>\n<p>I recommend copying this code and trying to remove or add these statements and see the changes yourself.</p>\n"}]
['https://Stackoverflow.com/questions/6414', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/357/']