If Else in C: Syntax, Evaluation Rules & Braced Blocks

Decision making in systems programming is built around conditional branching in c using if else. When an if else in c statement is written, the compiler tests the controlling expression enclosed in parentheses: If the result is any scalar (distinct from zero), the primary block is executed; If the result is zero, the optional else or else if clause is executed. Always use explicit braces for each branch of an if else to avoid the classic dangling-else ambiguity and accidental assignment bugs (typing a single equals sign instead of double equals), and use GCC warning flags such as -Wall and -Wextra.

If Else in C: Conditional Branching, Else-If Ladders & Dangling-Else Guardrails — Technical Overview

Decision making in systems programming is built around conditional branching in c using if else. When an if else in c statement is written, the compiler tests the controlling expression enclosed in parentheses: If the result is any scalar (distinct from zero), the primary block is executed; If the result is zero, the optional else or else if clause is executed. Always use explicit braces for each branch of an if else to avoid the classic dangling-else ambiguity and accidental assignment bugs (typing a single equals sign instead of double equals), and use GCC warning flags such as -Wall and -Wextra.

#include <stdio.h>

int main(void) {
    int score = 85;
    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 80) {
        printf("Grade: B\n");
    } else {
        printf("Grade: C\n");
    }
    return 0;
}

Runnable C example

#include <stdio.h>

int main(void) {
    int score = 85;
    if (score >= 90) {
        printf("Grade: A\n");
    } else if (score >= 80) {
        printf("Grade: B\n");
    } else {
        printf("Grade: C\n");
    }
    return 0;
}

Open example in compiler

Expected Output

Grade: B