beginner18 min

Control Flow: if, for, while, and switch

C's four control-flow keywords, and the switch fallthrough behavior to watch for.

What you'll learn

  • Write `if`/`else if`/`else` and both `for` and `while` loops
  • Explain why C's `switch` requires an explicit `break` to avoid falling through to the next case
  • Choose between `for` and `while` based on whether the number of iterations is known upfront

Explanation

C has four control-flow keywords covering conditionals and loops: if/else, for, while, and switch. Conditions are always wrapped in parentheses -- if (x > 0) { ... }, unlike languages that dropped the requirement.

A for loop is typically used when the number of iterations is known upfront: for (int i = 0; i < n; i++) { ... } bundles initialization, condition, and increment into one line. A while loop is preferred when the loop should continue based on a condition that isn't a simple counter: while (condition) { ... }. C also has do { ... } while (condition);, which always runs its body at least once before checking the condition -- useful when you need "run once, then repeat while true" semantics.

C's switch statement has a behavior that trips up many newcomers: cases fall through to the next case by default unless you explicitly write break. Without a break at the end of a matched case's block, execution just continues running the code of the next case too, even though its condition was never checked. This is different from languages like Go, which break automatically after each case -- in C, forgetting break is a classic, genuinely dangerous bug, not a stylistic quirk.

Guided lab

Fill in the blank: choosing while over for

CNot executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, fill in the missing piece, then reveal the completed code and its expected output.

Fill in the missing loop keyword, then predict the output.

#include <stdio.h>

int main(void) {
    int i = 1;
    ____ (i <= 5) {
        if (i % 2 == 0) {
            printf("%d even\n", i);
        } else {
            printf("%d odd\n", i);
        }
        i++;
    }
    return 0;
}

Stuck? Get a hint.

Common mistakes

  • Forgetting `break` at the end of a switch case and being surprised execution continues into the next case's code.
  • Using `=` (assignment) instead of `==` (equality) inside an `if` condition, which compiles fine and silently does the wrong thing.
  • Reaching for a `for` loop when the true stopping condition isn't a simple counter, making a `while` loop the clearer choice.

Knowledge check

Knowledge check

1. What happens if a matched switch case in C has no `break` statement?
2. Which loop always executes its body at least once before checking its condition?
3. In C, must an `if` condition be wrapped in parentheses?

Takeaway

Always write an explicit `break` at the end of each switch case you don't intend to fall through, and reach for `while` instead of `for` whenever the stopping condition isn't a simple counter.

Summary

C offers if/else, for, while, do-while, and switch; switch cases fall through without an explicit break, a common and genuinely dangerous beginner bug.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.