Learn C
#include "controlflow/if_else.h"

if, else if, else Statements in C

#concept

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.

#how it works
1

Nonzero means true

if (score >= 60) tests a comparison result (1 or 0); if (p) tests a pointer against NULL — any scalar works.

2

else if chains alternatives

Each else if tests only when everything above failed — first-match-wins ordering for grades, ranges, categories.

3

else catches the rest

A final else needs no condition and guarantees some path executes — the fallback for invalid or unexpected input.

4

Brace everything

Unbraced single statements invite dangling-else bugs when lines are added later — brace every branch unconditionally.

#example
#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;
}
Output
B
1score >= 90 is false (0) for 85, so the first block is skipped.
2score >= 80 is true (1), so B prints and the chain exits — else never runs.
3Exactly one branch executes; the chain guarantees one outcome.
#use cases
Grading, tiering, and range classification with ordered chains
Validating input with early if (bad) branches before real work
Testing pointers and counts directly as truthy/falsy values
Guaranteeing fallback behavior with a trailing else

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.