if, else if, else Statements in C
An if statement runs a block only when its condition is nonzero; else if chains test alternatives in order and a trailing else catches everything left over. Conditions accept any scalar — comparisons, integers, pointers (NULL means false) — which is expressive but demands care: if (x = 5) assigns instead of comparing.
Always brace branches, even single statements. The dangling-else ambiguity (which if owns this else?) resolves to the nearest if, but code that depends on readers knowing that rule is already too clever.
Nonzero means true
if (score >= 60) tests a comparison result (1 or 0); if (p) tests a pointer against NULL — any scalar works.
else if chains alternatives
Each else if tests only when everything above failed — first-match-wins ordering for grades, ranges, categories.
else catches the rest
A final else needs no condition and guarantees some path executes — the fallback for invalid or unexpected input.
Brace everything
Unbraced single statements invite dangling-else bugs when lines are added later — brace every branch unconditionally.
#include <stdio.h> int main(void) { int score = 85; if (score >= 90) { printf("A\n"); } else if (score >= 80) { printf("B\n"); } else { printf("C or below\n"); } return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C yourself — building beats watching.