Control Flow
Control Flow
Section titled βControl FlowβWhat it means
Section titled βWhat it meansβControl flow statements determine which code runs and how many times. C# offers if/else if/else for branching, switch (statement or expression) for multi-way branching on a single value, and for, foreach, while, and do-while for repetition.
Examples
Section titled βExamplesβint age = 20;
if (age < 13) Console.WriteLine("child");else if (age < 20) Console.WriteLine("teenager");else Console.WriteLine("adult");
// Switch statementswitch (age){ case < 13: Console.WriteLine("child"); break; case < 20: Console.WriteLine("teenager"); break; default: Console.WriteLine("adult"); break;}
// Switch expression (C# 8+) -- more concise, returns a valuestring category = age switch{ < 13 => "child", < 20 => "teenager", _ => "adult",};
for (int i = 0; i < 5; i++) Console.WriteLine(i);foreach (var item in new[] { "a", "b", "c" }) Console.WriteLine(item);Common mistake
Section titled βCommon mistakeβForgetting break in a traditional switch statement case β unlike some languages, C# does not allow implicit fall-through between non-empty cases, so a missing break is actually a compile error, not a silent bug, which is one of C#βs deliberate safety improvements over C/C++/JavaScript.
switch (age){ case 18: Console.WriteLine("just became an adult"); // Error: Control cannot fall through from one case label to another case 21: Console.WriteLine("can drink in the US"); break;}
// Fix: add break, or use goto case for intentional fall-throughQuick practice
Section titled βQuick practiceβ-
Does C#βs traditional
switchstatement allow implicit fall-through between cases like C or JavaScript does?Answer
No β C# requires an explicitbreak,return, orgoto caseat the end of every non-empty case; unintentional fall-through is a compile error, not a silent bug. -
Whatβs the difference between a switch statement and a switch expression?
Answer
A switch statement executes code per case usingbreak; a switch expression (C# 8+) directly evaluates to a value using=>arms, often more concise for simple value-selection logic. -
What does
foreachdo that a plainforloop over an index doesnβt?Answer
It iterates directly over the elements of anyIEnumerablecollection without manual index management, which is both simpler and works for collections that don't support indexed access.