Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Conditional Statements  ›  Lesson

if, elsif and else

if, elsif, else 12 minAn if statement runs only the first branch whose condition is truthy, and because it is an expression it returns that branch's value, or nil if nothing ran.

if, elsif and else

Bean Cart's cup sizes cost different amounts: small, medium and large. The script has to pick one price out of three. if with elsif and else is how a Ruby program chooses a path, and in Ruby that choice is itself a value you can store.

The rule

The first true branch wins, and the whole if returns its value.

The shape, with Ruby's unusual spelling elsif (no second "e"):

size = "medium"

if size == "small"
  price = 3.50
elsif size == "medium"
  price = 4.00
elsif size == "large"
  price = 4.50
else
  price = nil
end

Ruby checks the conditions from top to bottom and runs only the first branch whose condition is truthy. The rest are skipped, even if they would also be true. else catches everything that matched nothing. Every if closes with end. No parentheses are needed around the condition.

Because if is an expression, it returns the value of the branch that ran, so you can write the same thing without repeating price =:

price =
  if size == "small" then 3.50
  elsif size == "medium" then 4.00
  else 4.50
  end

(then lets you put a branch on the same line as its condition.) If no branch runs and there is no else, the if returns nil.

Two compact forms are common for simple cases:

puts "Free cookie!" if total >= 10          # modifier if
label = total >= 5 ? "stamp" : "no stamp"  # ternary: condition ? yes : no

Use the modifier form when the action is short and the condition is the less important part. Use the ternary only for a simple choice between two values; nested ternaries are hard to read.

The shortcut

Put the most specific condition first. Because the first true branch wins, a broad condition placed early swallows everything after it. Read the branches top to bottom and ask, for each one, "could an earlier branch already have caught this?"

Fixing Bean Cart's loyalty levelsStep 1 of 4
  1. The buggy code

    if stamps >= 1 then "member" elsif stamps >= 10 then "gold" end

Drills

Answer fast — trust the rule.

quick drill

How do you spell the "else if" keyword in Ruby?

quick drill

With total = 12, what is total > 5 ? "big" : "small"?

quick drill

With n = 15, what does if n > 10 then "A" elsif n > 5 then "B" else "C" end return?

quick drill

What does x = if false then 1 end leave in x?

quick drill

Which line prints "Free cookie!" only when total is at least 10?

Remember

  • The first true branch wins, and the whole if returns its value (or nil).
  • Put the most specific condition first so every branch can be reached.
  • Trap: spelling it else if, and a broad early condition that hides later branches.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThe unreachable branch

Bean Cart's code says if stamps >= 1 then "member" elsif stamps >= 10 then "gold" end. Explain why a customer with 12 stamps never becomes gold, fix it, and explain what the whole if returns.