← Back to Home

🚦 Conditionals: Think Traffic Lights

Traffic lights at intersection

Conditionals let your program make decisions based on different situations. Think of them as traffic lights - they control the flow based on conditions.

🚦 The Traffic Light Analogy

At an intersection, drivers make decisions based on the light color: IF green → GO, IF yellow → SLOW DOWN, IF red → STOP. Conditionals work the same way in code - they check conditions and execute different actions.

The IF Statement

The most basic conditional: "IF this is true, THEN do that"

Example: Weather App

IF temperature > 30°C
THEN display "It's hot! Stay hydrated"

IF-ELSE: Two Paths

Choose between two options:

Example: Login System

IF password is correct
THEN log user in
ELSE show error message

IF-ELSE IF-ELSE: Multiple Paths

Like a traffic light with multiple colors:

Example: Grade Calculator

IF score >= 90 → Grade A
ELSE IF score >= 80 → Grade B
ELSE IF score >= 70 → Grade C
ELSE → Grade F

Real-World Examples

Comparison Operators

How we check conditions:

Combining Conditions

Check multiple things at once:

The bottom line: Conditionals are how programs make decisions. Just like you decide what to wear based on the weather, programs use conditionals to respond to different situations. They're essential for creating smart, responsive applications!

Conditionals If-Else Logic