Phase 1 · C FoundationsModule 4~36 min read

Decision Making & Control Flow

Make programs choose with if/else, switch, Boolean logic, short-circuit evaluation, and the conditional operator.

What you'll learn

Control flow turns calculations into decisions. You'll learn how C interprets truth, choose among branches with if and switch, and use short-circuiting to express both logic and safety.

By the end, you'll be able to:

  • Build readable multi-branch decisions
  • Combine conditions without evaluating unsafe expressions
  • Use switch for discrete integral choices
  • Recognize assignment, fallthrough, and brace-related bugs

Truth in C

In a condition, zero is false and every nonzero scalar value is true. Comparison operators produce the integer value 0 or 1. Include <stdbool.h> when a variable represents a yes/no concept; bool communicates intent more clearly than a raw integer.

truth.c
#include <stdbool.h>
#include <stdio.h>

int main(void) {
    int items = 3;
    bool has_items = items != 0;

    printf("%d\n", 7 > 2);       // comparisons produce 0 or 1
    printf("%d\n", has_items);

    if (items) {                    // any nonzero value is true
        printf("Cart is not empty.\n");
    }
    return 0;
}
ExpressionMeaning
a == bequal
a != bnot equal
a < bless than
left && rightboth conditions are true
left || rightat least one condition is true
!conditioninvert truth

if, else if & else

An if statement selects a block when its condition is true. Anelse if chain checks from top to bottom and executes only the first matching branch. The final else handles everything remaining.

A two-way branch
score >= 60?
true
Print “Pass”
false
Print “Try again”
grades.c
#include <stdio.h>

int main(void) {
    int score = 84;
    char grade;

    if (score >= 90) {
        grade = 'A';
    } else if (score >= 80) {
        grade = 'B';
    } else if (score >= 70) {
        grade = 'C';
    } else if (score >= 60) {
        grade = 'D';
    } else {
        grade = 'F';
    }

    printf("Grade: %c\n", grade);
    return 0;
}

Tip

Order overlapping ranges from most specific or highest threshold to least. Ifscore >= 60 came first, an A score would match it before reaching the A branch.

Logic & short-circuiting

&& stops as soon as its left operand is false.|| stops as soon as its left operand is true. This is calledshort-circuit evaluation, and it lets an earlier condition guard a later operation.

guarded_division.c
#include <stdio.h>

int main(void) {
    int total = 120;
    int count = 0;

    // The division runs only when count is not zero.
    if (count != 0 && total / count >= 60) {
        printf("Average passes.\n");
    } else {
        printf("No passing average available.\n");
    }
    return 0;
}

Key idea

Put the safety check first. In count != 0 && total / count >= 60, C guarantees that division is skipped when count is zero.

switch statements

A switch compares one integral or enumeration expression against constantcase labels. It is ideal for menu commands, states, tokens, and other discrete choices.

commands.c
#include <stdio.h>

int main(void) {
    char command = 's';

    switch (command) {
        case 'n':
            printf("New file\n");
            break;
        case 's':
            printf("Save file\n");
            break;
        case 'q':
            printf("Quit\n");
            break;
        default:
            printf("Unknown command\n");
            break;
    }
    return 0;
}

Execution normally continues into the following case unless you use break,return, or another jump. That fallthrough is occasionally useful when several labels intentionally share one body:

fallthrough.c
#include <stdio.h>

int main(void) {
    int day = 6;

    switch (day) {
        case 6:
        case 7:
            printf("Weekend\n"); // 6 and 7 intentionally share this body
            break;
        default:
            printf("Weekday\n");
            break;
    }
    return 0;
}

Watch out

Accidental fallthrough is a classic C bug. Group empty labels as above or add an explicit comment when fallthrough is deliberate.

The conditional operator

The conditional expression condition ? value_if_true : value_if_false chooses one of two values. It works well for short assignments and arguments; use anif statement when branches perform several actions.

conditional.c
#include <stdio.h>

int main(void) {
    int temperature = 31;
    const char *label = temperature >= 30 ? "hot" : "comfortable";

    printf("Today is %s.\n", label);
    return 0;
}

Common decision-making pitfalls

  • Using assignment = when comparison == was intended
  • Omitting braces and later adding a misleadingly indented second statement
  • Comparing floating-point calculation results for exact equality
  • Writing overlapping conditions in the wrong order
  • Forgetting break in a switch
assignment_bug.c
#include <stdio.h>

int main(void) {
    int logged_in = 0;

    // Wrong: assignment makes logged_in equal 1, then tests true.
    if (logged_in = 1) {
        printf("This runs unexpectedly.\n");
    }

    // Correct:
    if (logged_in == 1) {
        printf("Logged in.\n");
    }
    return 0;
}

Let the compiler help

A warning-enabled compiler flags assignment used as a truth value. Parenthesize deliberate assignments in conditions and fix accidental ones immediately.

Recap & quick check

Key takeaways

  • C treats zero as false and nonzero scalar values as true.
  • An else-if chain executes the first matching branch only.
  • Logical AND and OR short-circuit, allowing earlier checks to guard later expressions.
  • switch is designed for discrete integral choices; break prevents unintended fallthrough.
  • Use the conditional operator for short value selection, not complex side effects.

Quick check

1. Which C values are considered true in a condition?

2. Why is count != 0 placed before division in an && expression?

3. What usually prevents execution from continuing into the next switch case?

4. What does = do inside a condition?

Your programs can now choose. Next up: Module 5 — Loops, where one clear block of code can process many values.