C Programming·The fundamentals of memory

Control flow and loops

Choose between paths with if/else and switch, and repeat work with for and while loops.

Beginner 12 min read Lesson 2 of 300 Challenge included
What you will be able to do
  • Branch with if/else and select one case with switch.
  • Choose for when the count is known and while when it is not.
  • Use break to exit and continue to skip a pass.
  • Spot off-by-one errors in loop bounds.

In plain English

Control flow decides which lines of a program actually run. An if statement runs a block only when its condition is true, and an else block runs when it is false. A switch statement picks one case out of several by matching a single value, and each case ends with break so the program does not fall through into the next one. Loops repeat a block: a for loop is best when you know how many times to run, and a while loop is best when you keep going until a condition becomes false. break exits a loop immediately, and continue skips the rest of the current pass and starts the next one. An off-by-one error happens when a loop bound is one too small or one too large, so it skips the last item or runs once too many.

Step by step

  1. 1Write an if/else that classifies a lesson length.
  2. 2Add a switch that matches a specific minute count.
  3. 3Count from 0 to 4 with a for loop.
  4. 4Write a while loop and skip one value with continue.
  5. 5Run the loop bounds by hand to check for an off-by-one.
control.cC
#include <stdio.h>

int main(void) {
    int minutes = 12;

    if (minutes > 10) {
        printf("long lesson\n");
    } else {
        printf("short lesson\n");
    }

    switch (minutes) {
        case 10:
            printf("ten minutes\n");
            break;
        case 12:
            printf("twelve minutes\n");
            break;
        default:
            printf("another length\n");
    }

    for (int i = 0; i < 5; i++) {
        printf("for %d\n", i);
    }

    int j = 0;
    while (j < 3) {
        if (j == 1) {
            j++;
            continue;
        }
        printf("while %d\n", j);
        j++;
    }

    return 0;
}

Common mistakes

  • Forgetting break in a switch case lets execution fall through into the next case by accident.
  • Using <= instead of < in a loop that starts at zero runs the body one time too many.
  • Placing the increment after continue can skip it, which makes the loop run forever.
  • Checking the loop variable after the loop has ended reads a value that may be out of scope.
Your turn

Sum the first five numbers

Easy

Use a for loop to add the numbers 0 through 4 and print the total, which should be 10.

C · starter
int total = 0;
for (int i = 0; i < 5; i++) {
}
printf("total = %d\n", total);
Check your understanding before moving on.
Knowledge checkQuestion 1

How many times does the loop `for (int i = 0; i < 5; i++)` run?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Functions and scope
Lesson 2 of 300 · 0% of course