If / Else
An if statement runs code only when a condition is true.
Think of a door that opens only when the password is right.
The condition must be a bool. Go will not guess for you.
Parentheses around the condition are optional. Most Go code leaves them out.
Basic if
Write if, then a true/false check, then braces.
Braces are required even for one line.
That keeps the code neat and helps avoid bugs.
if and else
else is the other path.
If the check is false, the else block runs.
else if chains
Chain else if to test several conditions in order.
The first true branch runs. The rest are skipped.
Think of picking the first matching door in a hallway.
Short statement before the check
Go lets you run a short statement before the condition: if x := value; condition.
That variable only lives inside the if/else block.
It is like a temporary sticky note for this choice only.
Keep conditions clear
Write checks that a human can read out loud.
If you have many branches, a switch may be easier.
Keep conditions clear. For many branches, try switch next.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.