Control Flow — if/else, switch, Loops
if/else if/else and switch, how to repeat work with for, while, and do-while loops, how break and continue control loop execution, and how to combine a loop with a conditional in a real worked example — FizzBuzz.
if / else if / else
An if statement runs a block of code only when its condition evaluates to true. Chain additional checks with else if, and catch everything else with a final else.
// grade.cpp #include <iostream> int main() { int score = 82; if (score >= 90) { std::cout << "Grade: A" << std::endl; } else if (score >= 80) { std::cout << "Grade: B" << std::endl; } else if (score >= 70) { std::cout << "Grade: C" << std::endl; } else { std::cout << "Grade: F" << std::endl; } return 0; }
score = 82, the first check (>= 90) fails, the second (>= 80) succeeds, so "Grade: B" prints and nothing after it runs.switch Statements
A switch compares one value against several possible case labels — a cleaner alternative to a long chain of else if checks when you're comparing the same variable against many exact values.
// day_name.cpp #include <iostream> int main() { int day = 3; switch (day) { case 1: std::cout << "Monday" << std::endl; break; case 2: std::cout << "Tuesday" << std::endl; break; case 3: std::cout << "Wednesday" << std::endl; break; default: std::cout << "Unknown day" << std::endl; } return 0; }
break, execution doesn't stop at the end of a matching case — it "falls through" and keeps running the code in the next case too, regardless of whether that case's value matches. This is honest C++ behavior, not a bug, but it's a very common source of accidental bugs when a break is missing.Intentional fallthrough — grouping cases
Fallthrough isn't always a mistake — it's sometimes used on purpose to group multiple case values under one block of code, by stacking case labels with no code (and no break) between them:
// weekday_check.cpp #include <iostream> int main() { int day = 6; switch (day) { case 1: case 2: case 3: case 4: case 5: std::cout << "Weekday" << std::endl; break; case 6: case 7: std::cout << "Weekend" << std::endl; break; default: std::cout << "Invalid day" << std::endl; } return 0; }
With day = 6, execution jumps straight to case 6, falls through the empty case 7 label (no code there to run), hits the shared std::cout line, prints "Weekend", and the break stops it there.
for Loops
A for loop packs initialization, condition, and update into one line — ideal when you know roughly how many times you want to repeat something.
// for_loop.cpp #include <iostream> int main() { for (int i = 1; i <= 5; i++) { std::cout << "i = " << i << std::endl; } return 0; }
int i = 1; — runs oncei <= 5; — checked every iterationi++ — runs after every iterationwhile and do-while Loops
A while loop checks its condition before each iteration — if it's false from the start, the body never runs. A do-while loop checks its condition after each iteration, so the body always runs at least once.
// while_loop.cpp #include <iostream> int main() { int n = 5; while (n > 0) { std::cout << n << std::endl; n--; } return 0; }
// do_while.cpp #include <iostream> int main() { int n = 0; do { std::cout << "n = " << n << std::endl; n++; } while (n < 3); return 0; }
do-while when you need the body to execute at least once no matter what — a classic example is a menu prompt: you want to show the menu once before checking whether the user chose to exit.break & continue
break exits the nearest enclosing loop (or switch) immediately. continue skips the rest of the current iteration and jumps straight to the next one, without exiting the loop.
// break_continue.cpp #include <iostream> int main() { for (int i = 1; i <= 10; i++) { if (i == 6) break; // stop the loop entirely at i == 6 if (i % 2 == 0) continue; // skip even numbers std::cout << i << std::endl; } return 0; }
This prints 1, 3, 5 — even numbers are skipped by continue, and the loop stops entirely once i reaches 6 because of break.
Worked Example — FizzBuzz
FizzBuzz combines a loop with nested conditionals: for numbers 1 to 20, print "Fizz" if divisible by 3, "Buzz" if divisible by 5, "FizzBuzz" if divisible by both, and the number itself otherwise.
// fizzbuzz.cpp #include <iostream> int main() { for (int i = 1; i <= 20; i++) { if (i % 15 == 0) { std::cout << "FizzBuzz" << std::endl; } else if (i % 3 == 0) { std::cout << "Fizz" << std::endl; } else if (i % 5 == 0) { std::cout << "Buzz" << std::endl; } else { std::cout << i << std::endl; } } return 0; }
i % 15 == 0 before the individual 3 and 5 checks ensures "FizzBuzz" prints for those numbers instead of just "Fizz" — order matters in an if/else-if chain.Lesson Summary
Let's recap everything you learned in this lesson:
if/else if/else runs the first matching branch and skips the rest.switch compares one value against several cases — always add break unless you intentionally want fallthrough.for loops pack init/condition/update in one line; while checks before each pass; do-while always runs the body at least once.break exits a loop immediately; continue skips to the next iteration.do { ... } while(cond); loop's body execute?continue; do inside a loop?for (int i = 0; i < 5; i++), how many times does the loop body run?while and do-while?Now it's your turn to write real code. Compile and run it with g++ or an online compiler.
Write a program called
evens.cpp that prints every even number from 1 to 50 (inclusive), one per line. Your output should start:
4
6
...
50
Rules: Use a
for loop and the % operator to check for even numbers. Do not hard-code the list — the loop must generate it.
💡 Show hints if you're stuck
- Loop with
for (int i = 1; i <= 50; i++) - Inside the loop, check
if (i % 2 == 0) - If true, print
iwithstd::cout << i << std::endl; - Bonus: try rewriting it starting the loop at
i = 2and stepping byi += 2instead — no if-check needed