Control Flow
Break and Continue
break stops a loop, while continue skips to the next loop step.
loop control
Loop control statements change how a loop proceeds.
Break and Continue
BreakContinue.cs
Replay: real traced execution (multi-file project)
using System;
class Program
{
static void Main()
{
int stopAt = 5;
int kept = 0;
int seen = 0;
for (int value = 1; value <= 7; value++)
{
if (value > stopAt)
{
break;
}
if (value % 2 == 0)
{
continue;
}
kept += value;
seen++;
}
Console.WriteLine($"stopAt={stopAt}");
Console.WriteLine($"seen={seen}");
Console.WriteLine($"kept={kept}");
}
}
using System;
class Program
{
static void Main()
{
int stopAt = 3;
int kept = 0;
int seen = 0;
for (int value = 1; value <= 7; value++)
{
if (value > stopAt)
{
break;
}
if (value % 2 == 0)
{
continue;
}
kept += value;
seen++;
}
Console.WriteLine($"stopAt={stopAt}");
Console.WriteLine($"seen={seen}");
Console.WriteLine($"kept={kept}");
}
}
using System;
class Program
{
static void Main()
{
int stopAt = 7;
int kept = 0;
int seen = 0;
for (int value = 1; value <= 7; value++)
{
if (value > stopAt)
{
break;
}
if (value % 2 == 0)
{
continue;
}
kept += value;
seen++;
}
Console.WriteLine($"stopAt={stopAt}");
Console.WriteLine($"seen={seen}");
Console.WriteLine($"kept={kept}");
}
}
stopAt ← 5, kept ← 0, seen ← 0
4{5 static void Main()6 {7 int stopAt→ 5 = 5; //@stopAt=3, 78 int kept→ 0 = 0;9 int seen→ 0 = 0;kept ← 1, seen ← 1
pass 1 of 611for (int value1 = 1; value <= 7; value++)12{13 if (value > stopAt)14 {15 break;16 }17 if (value % 2 == 0)18 {19 continue;20 }21 kept→ 1 += value1;22 seen→ 1++;23}All 6 passes — pass 1 is the card above pass valuestopAtkeptseen1 1 — 1 0 → 1 2 2 — — — 3 3 — 4 1 → 2 4 4 — — — 5 5 — 9 2 → 3 6 6 5 — — if (value % 2 == 0)
pass 1 of 216}17if (value2 % 2 == 0)18{19 continue;20}if (value % 2 == 0)
pass 2 of 216}17if (value4 % 2 == 0)18{19 continue;20}if (value > stopAt)
12{13 if (value6 > stopAt5)14 {15 break;16 }Console.WriteLine($"stopAt={stopAt}");
25 Console.WriteLine($"stopAt={stopAt5}");26 Console.WriteLine($"seen={seen3}");27 Console.WriteLine($"kept={kept9}");28}outputstopAt=5 seen=3 kept=9
stopAt ← 3, kept ← 0, seen ← 0
4{5 static void Main()6 {7 int stopAt→ 3 = 3;8 int kept→ 0 = 0;9 int seen→ 0 = 0;kept ← 1, seen ← 1
pass 1 of 411for (int value1 = 1; value <= 7; value++)12{13 if (value > stopAt)14 {15 break;16 }17 if (value % 2 == 0)18 {19 continue;20 }21 kept→ 1 += value1;22 seen→ 1++;23}All 4 passes — pass 1 is the card above pass valuestopAtkeptseen1 1 — 1 0 → 1 2 2 — — — 3 3 — 4 1 → 2 4 4 3 — — if (value % 2 == 0)
16}17if (value2 % 2 == 0)18{19 continue;20}if (value > stopAt)
12{13 if (value4 > stopAt3)14 {15 break;16 }Console.WriteLine($"stopAt={stopAt}");
25 Console.WriteLine($"stopAt={stopAt3}");26 Console.WriteLine($"seen={seen2}");27 Console.WriteLine($"kept={kept4}");28}outputstopAt=3 seen=2 kept=4
stopAt ← 7, kept ← 0, seen ← 0
4{5 static void Main()6 {7 int stopAt→ 7 = 7;8 int kept→ 0 = 0;9 int seen→ 0 = 0;kept ← 1, seen ← 1
pass 1 of 711for (int value1 = 1; value <= 7; value++)12{13 if (value > stopAt)14 {15 break;16 }17 if (value % 2 == 0)18 {19 continue;20 }21 kept→ 1 += value1;22 seen→ 1++;23}All 7 passes — pass 1 is the card above pass valuekeptseen1 1 1 0 → 1 2 2 — — 3 3 4 1 → 2 4 4 — — 5 5 9 2 → 3 6 6 — — 7 7 16 3 → 4 if (value % 2 == 0)
pass 1 of 316}17if (value2 % 2 == 0)18{19 continue;20}All 3 passes — pass 1 is the card above pass value1 2 2 4 3 6 Console.WriteLine($"stopAt={stopAt}");
25 Console.WriteLine($"stopAt={stopAt7}");26 Console.WriteLine($"seen={seen4}");27 Console.WriteLine($"kept={kept16}");28}outputstopAt=7 seen=4 kept=16
Keep Odd Values Until Stop
stopAtstarts at5.- The loop checks values from
1through7. - Even values use
continue, so they are skipped. - Values above
5usebreak, so the loop stops. - The kept total is
1 + 3 + 5, which is9. | Value | Action | Seen | Kept | | --- | --- | --- | --- | |1| add |1|1| |2| continue |1|1| |3| add |2|4| |4| continue |2|4| |5| add |3|9| |6| break |3|9|
Exercise: BreakContinue.cs
Use continue to skip even values, break after a stop value, and print seen and kept totals