C# · Control flow
Make choices with conditions. Repeat work with while, do while and for.
Workshop repo: git.fhict.nl/I872272/csharpbasics Last modified:
ifif elseif else ifwhile — pretestdo while — posttestfor — countedDiagrams and examples based on csharptutorial.net.
ifCondition true → run the if block. False → skip to End.
Syntax
if (condition)
{
// statements
}if condition:
# statementstrue.{ }, even for one statement.if — exampleExample
string condition = "sunny";
bool free = true;
if (condition == "sunny" && free)
{
Console.WriteLine("Let's go outside.");
}condition = "sunny"
free = True
if condition == "sunny" and free:
print("Let's go outside.")condition == "sunny" → truefree → true&& needs both true → block runsand needs both true → block runsif elseTrue → if block. False → else block. Always one of the two.
Syntax
if (condition)
{
// if statements
}
else
{
// else statements
}if condition:
# if statements
else:
# else statementsstring condition = "rainy";
if (condition == "sunny")
{
Console.WriteLine("Let's go outside.");
}
else
{
Console.WriteLine("Just stay home.");
}condition = "rainy"
if condition == "sunny":
print("Let's go outside.")
else:
print("Just stay home.")if else if / elifChecks conditions top to bottom. First true block wins; optional else is the fallback.
Syntax
if (condition1)
{
// block 1
}
else if (condition2)
{
// block 2
}
else if (condition3)
{
// block 3
}
else
{
// else block (optional)
}if condition1:
# block 1
elif condition2:
# block 2
elif condition3:
# block 3
else:
# else block (optional)if else if — BMI exampleBMI = Body Mass Index
weight in kg, height in m
double weight = 104;
double height = 2;
double bmi = weight / (height * height);
string weightStatus;
if (bmi < 18.5)
{
weightStatus = "Underweight";
}
else if (bmi >= 18.5 && bmi < 25)
{
weightStatus = "Healthy Weight";
}
else if (bmi >= 25 && bmi < 30)
{
weightStatus = "Overweight";
}
else
{
weightStatus = "Obesity";
}
Console.WriteLine(bmi);
Console.WriteLine(weightStatus);weight = 104
height = 2
bmi = weight / (height * height)
weight_status = None
if bmi < 18.5:
weight_status = "Underweight"
elif bmi >= 18.5 and bmi < 25:
weight_status = "Healthy Weight"
elif bmi >= 25 and bmi < 30:
weight_status = "Overweight"
else:
weight_status = "Obesity"
print(bmi)
print(weight_status)else catches everything elseSame result as the range version — different writing styles.
Only upper bounds
After a failed check, BMI is already ≥ that bound — so && is unnecessary.
if (bmi < 18.5)
{
weightStatus = "Underweight";
}
else if (bmi < 25)
{
weightStatus = "Healthy Weight";
}
else if (bmi < 30)
{
weightStatus = "Overweight";
}
else
{
weightStatus = "Obesity";
}if bmi < 18.5:
weight_status = "Underweight"
elif bmi < 25:
weight_status = "Healthy Weight"
elif bmi < 30:
weight_status = "Overweight"
else:
weight_status = "Obesity"Reordered (high → low)
Check the high end first; then the next branch is automatically below that bound.
if (bmi >= 30)
{
weightStatus = "Obesity";
}
else if (bmi >= 25)
{
weightStatus = "Overweight";
}
else if (bmi >= 18.5)
{
weightStatus = "Healthy Weight";
}
else
{
weightStatus = "Underweight";
}if bmi >= 30:
weight_status = "Obesity"
elif bmi >= 25:
weight_status = "Overweight"
elif bmi >= 18.5:
weight_status = "Healthy Weight"
else:
weight_status = "Underweight"Same ranges, different order
Same conditions as slide 7, only shuffled. Still correct — the ranges do not overlap.
if (bmi >= 25 && bmi < 30)
{
weightStatus = "Overweight";
}
else if (bmi < 18.5)
{
weightStatus = "Underweight";
}
else if (bmi >= 18.5 && bmi < 25)
{
weightStatus = "Healthy Weight";
}
else
{
weightStatus = "Obesity";
}if bmi >= 25 and bmi < 30:
weight_status = "Overweight"
elif bmi < 18.5:
weight_status = "Underweight"
elif bmi >= 18.5 and bmi < 25:
weight_status = "Healthy Weight"
else:
weight_status = "Obesity"Mutually exclusive ranges → order can change and the answer stays the same. Overlapping conditions → order becomes critical.
while (pretest loop)Check first. Body may run zero times if the condition starts false.
Syntax
while (expression)
{
// statement
}while expression:
# statementExample
int counter = 0; while (counter < 4) { counter++; Console.WriteLine(counter); }counter = 0 while counter < 4: counter += 1 print(counter)
Inspector
Output
do while (posttest loop)Body runs first. Condition is checked at the end — at least one iteration.
Syntax
do
{
// statement
} while (expression);while True:
# statement
if not expression:
breakExample
int counter = 0; do { counter++; Console.WriteLine(counter); } while (counter < 4);counter = 0 while True: counter += 1 print(counter) if not (counter < 4): break
Inspector
Output
whiledo while always runs the body at least once — condition is checked afterwards.while may run zero times if the condition starts false.while vs do whileint n = 10;
while (n < 4)
{
Console.WriteLine("while");
}
do
{
Console.WriteLine("do while");
} while (n < 4);n = 10
while n < 4:
print("while")
while True:
print("do while")
if not (n < 4):
breakwhile: maybe never — check firstdo while: at least once — menus, “try again?”, input until validfor loopInitializer once → check condition → body → iterator → check again.
Syntax
for (initializer; condition; iterator)
{
// statement
}for item in iterable:
# statement| Part | When |
|---|---|
initializer |
Once, before the loop |
condition |
Before each iteration |
iterator |
After each iteration |
Example
for (int i = 0; i < 3; i++) { Console.WriteLine(i); }for i in range(0, 3): print(i)
Inspector
Output
for — examplesExample
for (int i = 1; i < 10; i += 2)
{
Console.WriteLine(i);
}for i in range(1, 10, 2):
print(i)int total = 0;
for (int i = 1; i <= 4; i++)
{
total += i;
}
Console.WriteLine($"Total:{total}");total = 0
for i in range(1, 5):
total += i
print(f"Total:{total}")for (int i = 0; i < 10; i++)
{
if (i % 2 == 0)
{
Console.Write($"{i} ");
}
}for i in range(10):
if i % 2 == 0:
print(i, end=" ")%a % b is the remainder after dividing a by b.
17 % 5 → 2 — 5+5+5 = 15, leftover 210 % 3 → 1 — 3+3+3 = 9, leftover 1string message = "Hello";
for (int i = 0; i < message.Length; i++)
{
Console.WriteLine(message[i]);
}message = "Hello"
for ch in message:
print(ch)| Situation | Prefer |
|---|---|
| You know how many times (count, index, range) | for |
| Keep going while something is true — maybe zero times | while |
| Must run once (read input, then decide) | do whilewhile True + break |
| Choose between paths / ranges | if / if else / if else if |
You can nest them: a for with an if inside is normal.
if familyBranch on conditions. Prefer flat if else if over deep nesting.
while / doRepeat until a condition fails. Pretest vs posttest.
forCounted repetition:
init → check / condition → body / statement(s) → step / iteration.
Sources: if · if else · if else if · while · do while · for
Workshop repo: https://git.fhict.nl/I872272/csharpbasics Last modified: