Control Statements

Lesson 3 of 20

Give your code a brain: if, else and switch pick paths — watch marks become grades and a menu choose a drink.

The big picture

Let the code decide

So far every program ran top to bottom. Control statements let the data choose which lines run.

if

Runs its block only when the condition in ( ) is true — otherwise it is skipped.

else

The other path — runs when the if condition above it failed.

else if

Chain checks in a ladder: the first true condition wins, the rest are skipped.

switch

Compares one value against many case labels — tidy for menus and choices.

break & default

break exits the switch; default runs when no case label matched.