Decisions: if, else and switch

Introduction

An if statement runs its block only when its condition is true. In C, a condition is just a number: 0 means false and anything else means true.

Programs become useful when they can choose. Conditions in C are unusually simple, because they are only numbers, and that simplicity is the source of one of the language's best-known traps.

How it works

The = trap

if (x = 5) does not compare. It stores 5 in x, and since 5 is not 0 the block always runs. Real C accepts this silently; StepVioz stops and explains, because it is almost always a typo for ==.

switch

When you compare one value against many fixed choices, switch is clearer:

#include <stdio.h>

int main(void) {
    int day = 6;
    switch (day) {
    case 1:
        printf("Sunday: classes\n");
        break;
    case 6:
    case 7:
        printf("Weekend\n");
        break;
    default:
        printf("A weekday\n");
    }
    return 0;
}
Open in compiler

Each case needs a break, or C carries on into the next case. That "fall through" is occasionally useful, as in case 6: case 7: sharing one result.

Syntax

if (x > 0) {
    ...
} else if (x == 0) {
    ...
} else {
    ...
}

switch (n) {
case 1:
    ...
    break;
default:
    ...
}

Example

#include <stdio.h>

int main(void) {
    int marks = 75;
    char grade;
    if (marks >= 80) {
        grade = 'A';
    } else if (marks >= 70) {
        grade = 'B';
    } else if (marks >= 60) {
        grade = 'C';
    } else {
        grade = 'F';
    }
    printf("Marks %d: grade %c\n", marks, grade);
    if (marks >= 40 && marks <= 100) printf("Passed\n");
    return 0;
}
Open in compiler

C checks the conditions from the top. The first one that is true wins, and the rest are skipped. Step through and watch the calculation box: it shows each comparison with the real values, such as marks >= 80 → 75 >= 80 → false.

OperatorMeaning
== !=equal, not equal
< <= > >=comparisons
&&and: both must be true
||or: at least one must be true
!not

Line by line

  1. marks is 75, and grade has no value yet.
  2. marks >= 80 is false, so C goes on to the next test.
  3. marks >= 70 is true, so grade becomes 'B' and the rest of the chain is skipped.
  4. printf shows the marks and the grade.
  5. The last if uses &&, and both of its sides are true.

Expected result

Marks 75: grade B
Passed

In the real world

A thermostat, a login check and a game's rules are all built from conditions. A misplaced = inside an if has caused real security bugs, which is why compilers offer a warning for it.

Common mistakes

  • = instead of == in a condition.
  • A ; right after the condition: if (x > 0); ends the if there, so the block below always runs.
  • Forgetting break in a switch.
  • Writing if (0 < x < 10): C works out 0 < x first (0 or 1), then compares that with 10. Write x > 0 && x < 10.

Best practices

  • Always use braces, even for one line.
  • Compile with warnings turned on, such as -Wall.
  • Prefer switch when comparing one value with many fixed choices.

Summary

if chains run the first true branch. Comparisons give 0 or 1, and &&, || and ! combine them. == compares and = assigns. switch picks among fixed cases, each ended by break.

Quick check

1. In C, which value counts as false?
2. What is wrong with: if (n % 2 = 0)
3. What happens in a switch when a case has no break?

Key takeaways

  • In C, 0 is false and anything else is true.
  • Only the first true branch runs.
  • Each case in a switch needs a break.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.