Anatomy of an `if/else` Statement
This structure lets code choose between two paths. Click on the highlighted parts below to understand their roles.
Identify all 5 parts!
Predict the Path
Look at the code and the value of the variable. Which block of code (`if` or `else`) will run? Click the correct button.
score = 75
Build an `if/else` Statement
Drag the pieces into the dark area to build an `if/else` statement that checks if `count` is 0. Print "Empty" if it is, and "Not Empty" otherwise. Order and keywords matter! (Indentation is shown visually but not strictly enforced by drag placement).
Your Turn: Make a Decision
Write an `if/else` statement in the editor. Check if a variable named `age` is 18 or greater. If it is, print "Adult". Otherwise, print "Minor". We'll test it for you!
Mini Missions: Decisions!
Time for some quick coding challenges using `if/else`. Complete the tasks below.
Mission 1: Positive or Not?
Given a variable `number`, write an `if/else` statement. If `number` is greater than 0, print "Positive". Otherwise, print "Not positive".
Mission 2: Access Granted?
Given a variable `password`, write an `if/else` statement. If `password` is exactly equal to the string "open_sesame", print "Access Granted". Otherwise, print "Access Denied".
Mission 3: Even or Odd?
Given a variable `value`, write an `if/else` statement using the modulo operator (`%`). If `value % 2` equals 0, print "Even". Otherwise, print "Odd".
Module 7 Practice Complete!
Great job practicing `if/else` statements! You're getting better at controlling the flow of your code.
Ready for more complex decisions? Try the Advanced Practice for Module 7!