Skip to content
Fix Code Error

C# loop – break vs. continue

March 13, 2021 by Code Error
Posted By: Anonymous

In a C# (feel free to answer for other languages) loop, what’s the difference between break and continue as a means to leave the structure of the loop, and go to the next iteration?

Example:

foreach (DataRow row in myTable.Rows)
{
    if (someConditionEvalsToTrue)
    {
        break; //what's the difference between this and continue ?
        //continue;
    }
}

Solution

break will exit the loop completely, continue will just skip the current iteration.

For example:

for (int i = 0; i < 10; i++) {
    if (i == 0) {
        break;
    }

    DoSomeThingWith(i);
}

The break will cause the loop to exit on the first iteration – DoSomeThingWith will never be executed. This here:

for (int i = 0; i < 10; i++) {
    if(i == 0) {
        continue;
    }

    DoSomeThingWith(i);
}

Will not execute DoSomeThingWith for i = 0, but the loop will continue and DoSomeThingWith will be executed for i = 1 to i = 9.

Answered By: Anonymous

Related Articles

  • How does PHP 'foreach' actually work?
  • Callback functions in C++
  • problem with client server unix domain stream sockets…
  • useEffect Error: Minified React error #321 (GTM…
  • Adding gif image in an ImageView in android
  • How to find Control in TemplateField of GridView?
  • How to pass 2D array (matrix) in a function in C?
  • Why is 2 * (i * i) faster than 2 * i * i in Java?
  • Combining items using XSLT Transform
  • How to assign a value to an array inside a structure using…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How to do associative array/hashing in JavaScript

Next Post:

How to install Boost on Ubuntu

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error