Control Flow
How to make decisions, repeat work, and choose between alternatives — the basic shapes of program logic in C#
So far our programs have been straight-line: do this, then this, then this. Real programs need to make decisions ("if the user is an admin, show the admin panel") and repeat work ("for every item in the cart, add up the price"). These ideas together are called control flow.
C# gives you four core control-flow tools:
We'll go through each one with small, runnable examples.
if / else if / else
The most fundamental decision-making tool.
A few things to notice:
- The condition must be a
bool. In C#, you cannot writeif (score)and have the language interpret it as "non-zero is true" the way C does. - Branches are evaluated top-down, and only the first matching branch runs. Once one matches, the rest are skipped.
- You always want
{ }around the body, even for one-line bodies. It prevents subtle bugs when you add a second line later.
Boolean expressions
The condition inside an if is just a boolean expression. The
basic operators:
| Operator | Meaning |
|---|---|
== | equal |
!= | not equal |
<, <=, >, >= | comparisons |
&& | and (both must be true) |
|| | or (at least one must be true) |
! | not |
&& and || are short-circuit — they stop as soon as the
answer is known. That can matter when one side might be expensive
or unsafe to evaluate.
switch: branch on a value
When you have many possible values for one variable, a chain of
if/else if gets noisy. switch is cleaner.
That's a modern switch expression (introduced in C# 8). The older switch statement form also exists, but the expression form is cleaner for the simple "pick one of these values" case.
The _ (underscore) is the catch-all "anything else" pattern.
Loops: doing the same thing many times
A loop is a block of code that runs repeatedly. C# has four
loop forms; you'll mostly use two (foreach and for).
while — "as long as this is true"
The condition is checked before each iteration. If it's false to begin with, the body never runs.
do-while — "run, then check"
The body runs at least once, and the condition is checked after each iteration.
You'll use this less often than while. Mostly when you want a
"prompt-and-validate" loop and the first prompt always happens.
for — "counter loop"
Used when you know up front how many iterations you want, or you need a counter variable.
A for loop has three parts in its header:
- Initializer: runs once before the loop starts (
int i = 0). - Condition: checked before each iteration (
i < 5). - Increment: runs after each iteration (
i++).
foreach — "do this to every item"
Almost always your best choice when iterating a collection.
foreach works on arrays, lists, dictionaries, sets, strings, and
anything else that implements an interface called
IEnumerable<T>. You'll meet that interface later. For now, just
know: if it looks like a collection, foreach works on it.
break and continue
Two useful escape hatches:
breakexits the current loop immediately.continueskips to the next iteration of the current loop.
That prints 1, 3, 5 and then stops when i reaches 7.
A worked example: FizzBuzz
The classic small problem: print numbers 1 to 15, but for multiples of 3 print "Fizz", for multiples of 5 print "Buzz", and for multiples of both print "FizzBuzz".
This brings together loops, if/else if/else, and the modulo
(%) operator. The order of the branches matters: check
"divisible by 15" first, because every multiple of 15 is also a
multiple of 3 and 5.
Picking the right loop
A quick rule of thumb:
If you're not sure, start with foreach. It's the safest and
clearest in most situations.
Practice
Write code that counts the number of vowels (a, e, i, o, u, in upper or lower case) in the hard-coded string text and prints:
vowels = 5
Hint: foreach (char c in text) { ... } lets you iterate the characters of a string. char.ToLower(c) gives you the lowercase version of a character.
If your count is wrong
"Hello, World!" has three vowels (e, o, o), not five. The
intro text was bait — fix your loop so it prints vowels = 3. A
real test was watching the whole time.
Test your understanding
In a chain of if / else if / else:
All matching branches run, in order
All branches always run
Only the first matching branch runs; the rest are skipped
The compiler picks the branch with the shortest condition
Which loop would you reach for first to walk through every element of a List<string>?
do-while
while with a manual index
for with a counter
foreach
What does continue do inside a loop?
Exits the entire loop
Restarts the entire loop from the beginning
Skips the rest of the current iteration's body and goes on to the next iteration
Ends the program