ruby · level 1 · unit 4 · lesson 2
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
is the other path at the fork.
An if on its own either runs its lines or skips them. Often you want a plan B. else means otherwise: when the condition is false, Ruby runs the lines under else instead.
At a fork you always take exactly one path. With if and else, exactly one set of lines runs. Never both, and never neither.
else sits between the if lines and the end, lined up with the if. One end closes the whole thing.
biscuits = 0
if biscuits == 0
puts "Time to go to the shop."
else
puts "Have a biscuit!"
end Read else as otherwise.
guess first
Full marks or not? Make a guess.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
score = "10"
if score == 10
puts "Full marks!"
else
puts "Not quite."
end
your turn
Your turn. Meet a pet that is not a dog.
Type any pet except a dog between the quotes, then press run to see plan B.
fix it
London is not Paris, but the quiz says correct. Fix it!
- The answer is London, yet the quiz says correct. Look at the sign on line 2.
- One
=puts "Paris" into the box, and theifcounts that as a yes. To ask a question, you need two. - Change
=to==on line 2, then press run.
nailed it
your program has a plan b now. :D
elseruns when theifcondition isfalse.- Only one path runs: the
iflines or theelselines. - Ask questions with
==. One=stores a value.
new words in your collection
- else
This lesson isn’t saved. A free account remembers every lesson you finish.