Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Control Flow  ›  Lesson

if, else and switch

if else / switch 14 minif and else choose based on any condition, while switch jumps to the case whose integer constant matches a value and keeps running into later cases until it meets a break.

if, else and switch

The logger now has two decisions to make for every reading: which unit to print it in, Celsius or Fahrenheit, and whether to call it cold, mild or hot. They look alike, but they are different kinds of decision, and C has a different tool for each.

The rule

if tests any condition; switch jumps to the case matching one integer.

if / else runs a block when a condition is nonzero. Chain them with else if to test ranges or any mix of conditions:

if (t < 100) {
    printf("cold\n");
} else if (t < 250) {
    printf("mild\n");
} else {
    printf("hot\n");
}

Always use braces. Without them, an else attaches to the nearest unmatched if, whatever the indentation suggests.

switch compares one value against a list of constants and jumps to the matching case:

switch (unit) {
case 'C':
    printf("%.1f C\n", t / 10.0);
    break;
case 'F':
    printf("%.1f F\n", t / 10.0 * 9.0 / 5.0 + 32.0);
    break;
default:
    printf("unknown unit '%c'\n", unit);
    break;
}

The rules of switch:

  • The controlling value must have an integer type: int, char, an enum and so on. Not double, and not a string.
  • Each case label must be an integer constant expression, such as 3 or 'C', with no duplicates. A const int variable does not qualify in C.
  • After jumping to a case, execution runs straight on through the following cases until a break or the end of the switch. This is called fall-through.
  • default catches every value with no matching case. Include one.

The shortcut

Ask: "Am I comparing one integer with a list of exact values?" Yes: switch. Ranges, floating-point values, strings or several variables: an if / else if chain.

Tracing a switch with a missing breakStep 1 of 4
  1. The code

    switch (unit) { case 'C': print Celsius; case 'F': print Fahrenheit; break; } with no break after the Celsius line.

The compiler can help: GCC and Clang have -Wimplicit-fallthrough (GCC turns it on with -Wextra), which warns when one case runs into the next without a break.

When fall-through is deliberate, for example two cases sharing one action (case 'c': case 'C':), place the labels together. When there is code in between, a comment saying "falls through" tells readers it is intentional.

Drills

Answer fast — trust the rule.

quick drill

Can a switch test a double reading directly?

quick drill

What does switch (n) { case 1: printf("A"); case 2: printf("B"); break; default: printf("D"); } print when n is 1?

quick drill

In if (a) if (b) x = 1; else x = 2;, which if does the else belong to?

quick drill

Which is the right tool for "below 100 is cold, below 250 is mild, the rest is hot"?

quick drill

Given const int limit = 5;, is case limit: allowed in C?

Remember

  • if tests any condition; switch jumps to the case matching one integer constant.
  • Shortcut: one integer against exact values → switch; ranges or anything else → if / else if.
  • Trap: a missing break falls through, and an unbraced else joins the nearest if.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPrinting in the right unit

The logger prints a reading in Celsius or Fahrenheit depending on a char setting, and labels it cold, mild or hot by range. Explain which parts suit a switch and which suit an if/else chain, and what happens if you forget a break.