Cif / else Statements

if / else Statements

Decision-making is the foundation of every non-trivial program. C gives you the if, if / else, and if / else if / else statements to run different blocks of code depending on whether a condition is true or false. Unlike languages with a dedicated boolean type built in from the start, C evaluates any integer expression as a condition — zero means false, and any non-zero value means true.
Basic Syntax
An if statement runs a block of code only when its condition evaluates to true (non-zero). If the condition is false, the block is skipped entirely.

C
#include <stdio.h>

int main(void) {
    int age = 20;

    if (age >= 18) {
        printf("You are an adult.\n");
    }

    return 0;
}
if / else
Add an else branch to run alternate code when the condition is false.

C
int temperature = 15;

if (temperature > 25) {
    printf("It's warm.\n");
} else {
    printf("It's cool.\n");
}
Truthiness in C
C has no built-in boolean type in older standards (C99 added_Bool via <stdbool.h>). Any expression used as a condition is implicitly compared against zero: the value 0 is treated as false, and every other value — positive, negative, or a non-NULL pointer — is treated as true.

C
if (5) {
    printf("5 is truthy\n");   // runs
}

if (-1) {
    printf("-1 is truthy\n");  // runs
}

if (0) {
    printf("never prints\n");  // does not run
}
Note
A very common beginner bug is writing if (x = 5) instead of if (x == 5). This assigns 5 to x and then tests it as a condition — since 5 is non-zero, the branch always runs. Most compilers warn about this with-Wall; always compile with warnings enabled.
Braces vs. Single-Statement Bodies

C allows you to omit the curly braces when an if or else body is a single statement — only the statement immediately following the condition belongs to that branch. Everything after it runs unconditionally, which surprises many beginners.

C
// Dangerous: looks like both lines are guarded, but they are not.
if (age >= 18)
    printf("Adult\n");
    printf("Welcome!\n");  // Runs ALWAYS, regardless of age!

// Safe: braces make the intent explicit and immune to this mistake.
if (age >= 18) {
    printf("Adult\n");
    printf("Welcome!\n");
}
Warning
Always use braces { } around if/else bodies, even for a single statement. This avoids the classic dangling-else bug and protects you from accidentally changing a branch's scope when you (or a teammate) add a second statement later without noticing the missing braces.
The Dangling-Else Problem
When an else could grammatically attach to more than one if, C's rule is that it always binds to thenearest unmatched if. This can produce logic that looks correct by indentation but behaves differently.

C
int a = 1, b = 0;

// Indentation suggests the else belongs to the OUTER if,
// but it actually binds to the INNER if (nearest unmatched if).
if (a == 1)
    if (b == 1)
        printf("both true\n");
    else
        printf("a is 1, b is not 1\n");  // this runs, even though a == 1

// Braces remove all ambiguity:
if (a == 1) {
    if (b == 1) {
        printf("both true\n");
    }
} else {
    printf("a is not 1\n");
}
Key Points
  • A condition is true if it evaluates to any non-zero value, and false if it evaluates to exactly 0.

  • Without braces, only the single statement right after the condition belongs to that branch.

  • The dangling else always binds to the nearest unmatched if; use braces to make intent explicit.

  • Watch out for = vs == in conditions; assignment in a condition is valid C and usually a bug.

  • Compile with -Wall so the compiler flags suspicious conditions like accidental assignments.