break and continue
In this page:
break and continue
break immediately exits the nearest enclosing loop or switch, while continue skips the rest of the current iteration and jumps to the next one. Both give you finer control inside loops beyond what the loop condition alone provides. Overusing them can make control flow harder to follow, so use them sparingly.
Note: Use continue to skip invalid items early instead of nesting the rest of the loop body in an if.
Example: break and continue
using System;
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
continue; // skip even numbers
if (i > 7)
break; // stop once we pass 7
Console.WriteLine(i);
}
}
}
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: