Control Flow (if-else, switch)

Lesson 3 of 20

Teach your program to make choices — if, else, else-if ladders and switch, with every branch animated.

The big picture

Making decisions

Programs get interesting when they choose. if/else picks a path, ladders pick among many, switch jumps straight to a match.

if / else

Runs a block only when a condition is true. else catches every other case.

else-if ladder

Chain tests top to bottom; the first true branch wins and the rest are skipped.

switch

Compare one value against many cases — jump straight to the match, then break out.

Conditions are numbers

Comparisons in C produce 1 for true and 0 for false. Any non-zero value counts as true.