Making Choices with if/else
Branch on conditions.
Making Choices with if/else is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Code That Decides
Programs often need to choose a path. The if statement runs a block only when its condition is true, so your code can react to data. 🔀
Your First if
Write if, a condition, a colon, then an indented block. If the condition holds, Mojo runs the block; otherwise it skips it entirely.
if score > 90:
print("Top marks!")Conditions Are Booleans
The bit after if must boil down to True or False. Comparisons like greater-than and equals naturally produce a boolean for you.
var ready = count == 0
if ready:
print("Empty")Add an else Branch
Pair else with if to cover the other case. Exactly one of the two blocks runs, so nothing is ever left unhandled.
if temp > 30:
print("Hot")
else:
print("Mild")Indentation Defines the Block
Like Python, Mojo uses indentation to mark what belongs to a branch. Consistent spaces, usually four, keep the structure clear and valid.
Comparison Operators
Build conditions with operators like ==, !=, <, and >=. Each compares two values and hands back the boolean that if needs.
if age >= 18:
print("Adult")Chain More Cases with elif
Need more than two paths? Insert one or more elif branches between if and else to test extra conditions in order.
if g >= 90:
print("A")
elif g >= 80:
print("B")
else:
print("C")Only the First Match Wins
Mojo checks branches top to bottom and stops at the first condition that is true. The rest are skipped, even if they would also match.
Combine Conditions with and / or
Join checks using and and or. With and both must be true; with or just one is enough to enter the block.
if hungry and has_food:
print("Eat now")Flip a Condition with not
Put not in front of a condition to invert it. It turns True into False, handy for checking the absence of something.
if not logged_in:
print("Please sign in")Nesting Branches
You can place an if inside another branch to refine a decision. Keep nesting shallow so the logic stays easy to read.
if signed_in:
if is_admin:
print("Dashboard")Quick Check
You want to handle three or more separate cases in one decision. Which keyword do you reach for?
Recap
You branch with if, add else for the other case, and chain elif for more. Conditions are booleans you can combine with and, or, and not. 🎯
Frequently asked questions
Is the “Making Choices with if/else” lesson free?
Yes — the full text of “Making Choices with if/else” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Making Choices with if/else”?
Branch on conditions. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Making Choices with if/else” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Making Choices with if/else
- Looping with while
- Iterating with for and range
- break, continue, and Early Exit