Python Branching Decisions: if, else if, and else
Programs make intelligent choices through conditional branching. The if statement tests a condition; if true, its code block executes. If false, it cascades down to optional else if checks or the catch-all else block.
"An if/else statement is like a train track switch. If the signal is green (condition is true), the train rolls down Track A. Otherwise (else), the switch flips and the train rolls down Track B."
Deep Dive: How It Works
Comparison Operators: == (is equal to), != (not equal to), > (greater than), < (less than), >= (greater or equal), <= (less or equal).
The Assignment vs Equality Danger: Single = assigns a value; double == checks equality! Writing if (x = 5) always evaluates to true and overwrites x!
Curly Braces: While single statements don't strictly require braces, always use { } to avoid dangerous indentation bugs.
Syntax Blueprint
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else {
grade = 'C';
}Sequential conditional evaluation from top to bottom.
Core Rules to Remember



Common Beginner Traps & How to Fix Them
Writing if (x = 10) instead of if (x == 10)Why it happens: Typo of single equal sign.
How to fix: Remember: = is assignment, == is equality test.
Live Interactive Example
Hit Run Code to see it liveYour Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Check number positivity
Declare int num = -5;
Write an if/else checking if num > 0 (print "Positive"), else if num < 0 (print "Negative"), else (print "Zero").
Finished reading and practicing?
Mark this lesson as completed to update your course progress.