Control Flow

Ruby is a dynamic, beginner-friendly programming language, and control flow is how your program makes decisions — running different code depending on the situation.

Learn Control Flow in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll branch with if/elsif/else, read clearly with unless, match many cases with case/when, and write compact ternaries.

What You'll Learn in This Lesson

1️⃣ if, elsif, else, and unless

An if runs its block only when the condition is truthy; elsif adds more tests and else is the fallback. Always close the block with end . Ruby adds unless (the opposite of if ) and lets you trail a condition after a statement for tidy one-liners.

Remember Ruby's rule: only false and nil are falsy. 0 and "" are both truthy, which surprises newcomers.

2️⃣ case/when and the Ternary

When you're comparing one value against many options, case/when is cleaner. A single when can list several values or match a range . And since if / case are expressions, you can assign their result. For a quick two-way choice, the ternary cond ? a : b is perfect.

Your turn. Price a movie ticket from an age. Replace the two TODO prices, then run it for a few ages.

The classic interview warm-up, for a single number. Use modulo to test divisibility, and remember to check "both" first. Run with ruby fizz.rb .

📋 Quick Reference — Control Flow

Practice quiz

Which keyword runs its block only when the condition is FALSE?

  • if
  • unless
  • while
  • until

Answer: unless. unless is the opposite of if — it runs when the condition is false, reading like plain English.

In Ruby, which values are falsy?

  • false and nil only
  • false, nil, and 0
  • false, nil, and ""

Answer: false and nil only. Only false and nil are falsy. 0, "", and [] are all truthy.

What does return?

  • "child"
  • "teen"
  • "adult"
  • nil

Answer: "adult". 25 falls outside both ranges, so the else branch gives "adult".

How do you spell the 'else-if' keyword in Ruby?

  • elseif
  • elif
  • elsif
  • else if

Answer: elsif. Ruby spells it elsif — no second 'e'.

What is the result of the ternary ?

  • "hot"
  • "cool"
  • true
  • 30

Answer: "hot". 30 > 25 is true, so the ternary returns the value before the colon: "hot".

What does assign to grade?

  • nil
  • "A"
  • "B"
  • true

Answer: "A". if is an expression that returns the value of the branch that ran — here "A".

Which operator is used for a single when-branch matching multiple values, e.g. ?

  • ==
  • ===
  • =
  • eql?

Answer: ===. case/when uses the === operator under the hood, which is why ranges and multiple values work.

What does print?

  • nothing
  • Keep trying
  • an error
  • true

Answer: Keep trying. 80 >= 90 is false, so unless runs and prints "Keep trying".

What error usually means an if/case block is missing its closing keyword?

  • NoMethodError
  • syntax error, unexpected end-of-input
  • NameError
  • ArgumentError

Answer: syntax error, unexpected end-of-input. Every if/case needs a matching end; a missing one gives 'unexpected end-of-input'.

Using instead of inside does what?

  • Compares x to 5
  • Assigns 5 to x and is always truthy
  • Raises a syntax error
  • Returns nil

Answer: Assigns 5 to x and is always truthy. A single = assigns; sets x to 5 and is always truthy. Use == to compare.