ruby · level 1 · unit 4 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
An if runs code only when its is true.
Picture a fork in the path with a sign: Is it Saturday? An if asks a condition like that. If the answer is yes, Ruby runs the lines inside. If not, it walks straight past them.
Ruby answers every condition with true or false. A value that can only be one of these two is a . To ask whether two values match, use two equals signs, ==. One = puts a value in a box.
The word closes the if, marking where its lines stop. Indent the lines inside by two spaces. Ruby does not need the spaces, but they help you see what belongs to the if.
day = "Saturday"
puts day == "Saturday"
if day == "Saturday"
puts "Pancakes for breakfast!"
end
puts "Time to get dressed." Lines after the end sit outside the if, so they run every time.
guess first
Rain or shine? Guess what this shows.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
weather = "sunny"
if weather == "rainy"
puts "Take a coat."
end
puts "Off you go!"
your turn
Your turn. Get past the robot door.
The robot only opens for its owner. Type your name in both boxes, spelled exactly the same, then press run.
fix it
The secret door will not open. Can you fix it?
- Read the error. It is a SyntaxError, so Ruby could not read the code. What is missing at the bottom?
- Every
ifneeds a word that closes it, marking where its lines stop. - Add
endon a new line at the bottom, then press run.
nailed it
your code can make choices now. :D
ifruns its lines only when the condition istrue.- Use
==to ask whether two values match. - Every
ifneeds anend.
new words in your collection
- condition
- boolean
- end
This lesson isn’t saved. A free account remembers every lesson you finish.