views:

27444

answers:

8

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;
}
}
+8  A: 

break would stop the foreach loop completely, continue would skip to the next DataRow

palmsey
+83  A: 

break will exit the loop completely, continue will just stop 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.

Michael Stum
+13  A: 

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

JeremiahClark
+5  A: 

There are more than a few people who don't like break and continue. The latest complaint I saw about them was in JavaScript: The Good Parts by Douglas Crockford. But I find that sometimes using one of them really simplifies things, especially if your language doesn't include a do-while or do-until style of loop.

I tend to use break in loops that are searching a list for something. Once found, there's no point in continuing, so you might as well quit.

I use continue when doing something with most elements of a list, but still want to skip over a few.

The break statement also comes in handy when polling for a valid response from somebody or something. Instead of:

Ask a question
While the answer is invalid:
Ask the question

You could eliminate some duplication and use:

While True:
Ask a question
If the answer is valid:
break

The do-until loop that I mentioned before is the more elegant solution for that particular problem:

Do:
Ask a question
Until the answer is valid

No duplication, and no break needed either.

yukondude
+1  A: 

Ruby unfortunately is a bit different. PS: My memory is a bit hazy on this so apologies if I'm wrong

instead of break/continue, it has break/next, which behave the same in terms of loops

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

a = 5
while a < 10
a + 1
end

You can however do this

a = 5
b = while a < 10
a + 1
end # b is now 10

HOWEVER, a lot of ruby code 'emulates' a loop by using a block. The canonical example is

10.times do |x|
puts x
end

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. break/next mean different things in the context of a block.

break will jump out of the code that called the block

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.

def timesten
10.times{ |t| puts yield t }
end


timesten do |x|
x * 2
end
# will print
2
4
6
8 ... and so on


timesten do |x|
break
x * 2
end
# won't print anything. The break jumps out of the timesten function entirely, and the call to `puts` inside it gets skipped

timesten do |x|
break 5
x * 2
end
# 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

timesten do |x|
next 5
x * 2
end
# this would print
5
5
5 ... and so on, because 'next 5' skips the 'x * 2' and 'returns' 5.

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 :-)

Orion Edwards
+3  A: 

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:;
}
SemiColon
A: 

Simple answer:

Break exits the loop immediately.
Continue starts processing the next item. (If there are any, by jumping to the evaluating line of the for/while)

Maltrap
+2  A: 

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, adn there were more code tasks in between the next row, so i just let it fall thru.