Skip to content

Exercise 4 of 10 · Conditions

Umbrella or Not

What you will make

A weather forecast that prints one of three different messages, decided automatically from a single number.

The one new idea: if / elsif / else chooses between several outcomes

Almost every real program has to behave differently depending on the data it is given. if / elsif / else is how Ruby makes that choice, and getting the order and the conditions right is a skill worth building early.

Go straight to the code ↓

Choosing between more than two outcomes

if alone chooses between running a block of code or not. Adding else gives a program exactly two paths: this or that. Real decisions are often not that simple — a forecast is not just rain or no rain, it is a whole range of chances that deserve different advice.

elsif slots extra branches in between if and else. Ruby checks them from top to bottom, in order, and the moment one condition is true it runs that branch and skips every branch below it — even ones that would also have been true. Only one end, right at the bottom, closes the entire chain.

Ruby
score = 72

if score >= 90
  puts "Grade: A"
elsif score >= 75
  puts "Grade: B"
else
  puts "Grade: C"
end

score is 72, which fails the first test (not 90 or above) and the second (not 75 or above), so it falls all the way to else and prints Grade: C.

A worked example

Ruby
temperature = 15

if temperature >= 25
  puts "Warm enough for shorts."
elsif temperature >= 10
  puts "A jumper will do."
else
  puts "Coat weather."
end
Output
A jumper will do.

15 fails the first check, passes the second, and Ruby stops looking the moment it finds a true condition — the else never even gets asked.

Your turn

The forecast in the editor should give three different messages depending on chance_of_rain, currently set to 55. Press Run first: it prints Leave the umbrella at home, which is wrong for a 55% chance of rain.

The elsif line is a copy of the if line above it, so it can never be the branch that runs — whatever fails the first check fails the second one too, identically worded. Change the number in the elsif line to a lower threshold, one that 55 clears but that still leaves room below the if line's 70.

If something goes wrong

If Ruby refuses to run the file at all, check for a missing or misspelled elsif, or an extra end that does not belong. If the wrong message keeps printing, read the three conditions from the top down, the same order Ruby does, and ask which one is the first to be true for 55 — that branch, and only that branch, is the one that will run.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Writing elseif or else if as two words
Ruby's keyword is one word, elsif, with no second e. Anything else is a syntax error, and the error points at a line that looks perfectly reasonable to read.
Putting the widest condition first
If chance_of_rain >= 30 were the first check instead of the last, every number 30 and above would stop there, and the >= 70 branch further down would never run at all, no matter how high the number got.
Adding an end after each branch
Only the whole if needs one end, at the very bottom, closing if, every elsif and else together. Adding extra ends produces a syntax error about an unexpected keyword, often on a line far from the real problem.
Using = instead of >=
if chance_of_rain = 70 assigns 70 to the variable rather than comparing it, and Ruby warns found = in conditional, should be ==. The chosen branch also stops responding to the variable's real value, because the variable was just overwritten.

Want a blank editor instead? Open the Ruby playground.