If/Else: Choose Your Adventure! 🗺️✨
Make your programs do different things based on what's happening - just like a choose-your-own-adventure book!
🤔 What is a Conditional?
A conditional is how your computer makes decisions! It asks a question, and based on the answer, it does different things.
Like a Choose-Your-Own-Adventure Book!
"You find a mysterious door. Do you want to open it?"
🌍 You Make Decisions Every Day!
You use "if/then" thinking all the time!
IF it's raining
THEN bring umbrella
ELSE wear sunglasses
IF I'm hungry
THEN eat a snack
ELSE keep playing
IF light is green
THEN walk
ELSE wait
IF it's bedtime
THEN go to sleep
ELSE keep having fun
✨ How IF Works in Code
The computer asks: "Is this TRUE or FALSE?"
if (age >= 13)
"You can watch this movie!"
If age is 13 or more
The code runs!
If age is less than 13
Skip it!
🔀 IF / ELSE: Two Paths!
Sometimes you want to do something ELSE if the answer is NO:
Is it sunny? ☀️
Go to the beach!
Play video games!
if (sunny == true)
"Go to the beach!"
else
"Play video games!"
🌈 Many Choices: IF / ELSE IF / ELSE
What if you have MORE than two options? No problem!
🎯 Grade Checker
IF score >= 90: "Amazing! A+"
ELSE IF score >= 80: "Great! B"
ELSE IF score >= 70: "Good! C"
ELSE: "Keep practicing!"
if (score >= 90)
"Amazing! A+"
else if (score >= 80)
"Great! B"
else
"Keep practicing!"
🎮 Conditionals in Games!
Games are FULL of if/else decisions!
Health Check
IF health == 0 THEN game over!
Door Lock
IF hasKey THEN open door ELSE "It's locked!"
Level Up
IF points >= 100 THEN go to next level!
Enemy AI
IF player nearby THEN chase ELSE patrol
🔍 Asking Questions in Code
Here are the symbols we use to ask questions:
Is equal to?
age == 10
Is NOT equal?
color != "red"
Greater than?
score > 100
Less than?
time < 60
Greater or equal?
coins >= 50
Less or equal?
health <= 0
⭐ Remember This!
- 1
IF asks a yes/no question
- 2
ELSE is what happens when the answer is NO
- 3
ELSE IF lets you check more questions
- 4
Conditionals help your code make smart decisions!