Categories
c# enumeration loops

C# loop – break vs. continue

847

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;
    }
}

3

1577

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.

2

  • 4

    Why are their no braces around continue – I know that it works without them, but why?

    Jul 8, 2016 at 16:06

  • 62

    @GeorgeWillcox My younger, more foolish self hadn’t yet learned the value of using braces, always. (They are optional in C# for single statements, but not putting them makes it easier to introduce a bug later on. Also see programmers.stackexchange.com/a/16530/6342)

    – Michael Stum

    Jul 8, 2016 at 17:20


396

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.

break loop – looping is broken and stops.

continue loop – loop continues to execute with the next iteration.

0

    106

    break causes the program counter to jump out of the scope of the innermost loop

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

    Works like this

    for(i = 0; i < 10; i++)
    {
        if(i == 2)
            goto BREAK;
    }
    BREAK:;
    

    continue jumps to the end of the loop. In a for loop, continue jumps to the increment expression.

    for(i = 0; i < 10; i++)
    {
        if(i == 2)
            continue;
    
        printf("%d", i);
    }
    

    Works like this

    for(i = 0; i < 10; i++)
    {
        if(i == 2)
            goto CONTINUE;
    
        printf("%d", i);
    
        CONTINUE:;
    }
    

    3

    • 2

      I notice that many loops here just use the standard for loop, but will this work for pre and post tested while loops? The goto notation leads me to believe so, but need some verification.

      Aug 8, 2013 at 3:02

    • 1

      @Daniel, for WHILE loops you can use BREAK and CONTINUE, no problem.

      – Amila

      Apr 21, 2015 at 5:09


    • @DanielPark For both while (“pre-tested”) and dowhile (“post-tested”), after a continue; statement is met the next thing that will happen is that the loop condition is evaluated to decide whether an additional iteration is to be done. Of course, with break; the loop condition is not checked, the loop is simply exited completely. So the goto analogy is good for understanding this aspect too.

      Dec 29, 2017 at 14:16