Loading lessons...
Break & Continue
Break & Continue
The break and continue statements control the flow inside loops.
break
break stops the loop completely and jumps to the code after it:
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
Console.WriteLine(i);
}
This prints 0 1 2 3 then stops when i is 4.
continue
continue skips the rest of the current loop pass and moves to the next one:
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
Console.WriteLine(i);
}
This prints all numbers from 0 to 9 except 4 - it skips only that one pass.
break vs continue
break- exits the loop entirely.continue- skips one iteration, keeps looping.
Also in switch
You've seen break in switch statements too - it ends the case block.
TL;DR
breakstops the whole loop.continueskips just the current iteration.- Both are often combined with
ifconditions.