← C roadmap

Topic 3 of 15 · Beginner

Control Flow & Loops

if/else, switch, for, while, do-while, break and continue.

Concept

if/else chooses a branch; switch selects among integer/char cases and falls through unless you write break.

for loops for known counts, while for condition-driven repetition, do-while when the body must run at least once.

In C, any non-zero value is true and zero is false.

Code (C17)

#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 15; i++) {
        if (i % 15 == 0) printf("FizzBuzz\n");
        else if (i % 3 == 0) printf("Fizz\n");
        else if (i % 5 == 0) printf("Buzz\n");
        else printf("%d\n", i);
    }

    char grade = 'B';
    switch (grade) {
        case 'A': printf("Excellent\n"); break;
        case 'B': printf("Good\n"); break;
        default:  printf("Keep going\n");
    }
    return 0;
}

Remember

  • Missing break = fall-through.
  • switch works on integral types only.
  • Non-zero is true.

Interview questions

Can switch use strings in C?

No, only integral types (int, char, enum). Use if/else with strcmp for strings.

Print a pyramid pattern.

Outer loop over rows i; inner loops print (n − i) spaces then (2i − 1) stars, then a newline.

Practise C with the AI mentor

Lesson test

Take an 8-question test on “Control Flow & Loops” — concepts, output prediction, bug spotting and interview questions.

Can't find a C topic you need for exams or placements? Request it and we'll add it.