break vs continue
Overview
Section titled โOverviewโbreak
exits the nearest loop or switch. continue
skips to the next iteration of the nearest loop.
Side-by-side
Section titled โSide-by-sideโKeyword | What it does | Typical use |
---|---|---|
break | Exit loop/switch immediately | Found target, error condition |
continue | Skip rest of current iteration | Filter items, skip invalids |
Example
Section titled โExampleโfor (var i=0; i<10; i++){ if (i == 7) break; // stop entirely if (i % 2 == 0) continue; // skip evens Console.WriteLine(i);}
Flow diagram
Section titled โFlow diagramโ+---------+ +-----------+ +---------+ | start i | ---> | condition | ---> | body | +---------+ +-----------+ +---------+ / \ continue break (next iter) (exit loop)