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; } }
breakwill exit the loop completely,continuewill just skip the current iteration.For example:
The
breakwill cause the loop to exit on the first iteration —DoSomeThingWithwill never be executed.While:
Here
continueskips to the next iteration of the for-loop, meaningDoSomeThingWithwill not execute fori == 0.But the loop will continue and
DoSomeThingWithwill be executed fori == 1toi == 9.