Conditionals and Loops in Ruby
Fast recall
Quick revision
Core answer
if/elsif/else, unless, and case to choose which expression to evaluate. It supports while and until, but collection work is usually clearer with iterators such as each, map, select, and times. Only false and nil are falsey in Ruby; values such as 0 and an empty string are truthy.Whether you know if/unless/case and that Ruby prefers iterators (each/times/upto) over while counters.
Using a while counter when each/times would do (un-idiomatic), or confusing unless with if (unless = if not).
Conditionals: if/elsif/else, unless (if not), case/when. Loops: while/until, but prefer iterators (each/times/upto). break/next/redo.
Interview-ready guide
Interview answer
Ruby uses if, elsif, else, unless, and case to choose which code runs. These constructs are expressions, so the selected branch also becomes a value. For example, status = if paid then :ready else :waiting end assigns the Symbol returned by the chosen branch.
A case expression asks each when candidate to match using ===. A Range can therefore test membership, a Class can match an instance, and a regular expression can match text. If no branch matches and there is no else, the expression returns nil.
Ruby supports while and until, but collection work is usually clearer with an iterator. Use each for side effects, map to transform every item, select to filter, find for the first match, and times for a fixed count. The complete example lets branching and iteration produce useful values.
Only nil and false are falsey in Ruby. Zero, an empty String, and an empty collection are all truthy, so test emptiness with methods such as empty?. Inside repetition, next skips the rest of one iteration and break exits the nearest loop. Keep while for repetition driven by changing state rather than a known collection.
Conditionals and Loops in Ruby
Direct Answer
Ruby uses if/elsif/else, unless, and case to choose which expression to evaluate. It supports while and until, but collection work is usually clearer with iterators such as each, map, select, and times. Only false and nil are falsey in Ruby; values such as 0 and an empty string are truthy.
Deep Dive & Architecture
The important Ruby idea is that control-flow constructs evaluate to an object. In status = if paid then :ready else :waiting end, the chosen symbol becomes the assignment value. If no condition matches and there is no else, the result is nil. This makes a separate pre-declared result variable unnecessary in many cases.
A case expression sends === to each when candidate. A Range therefore checks membership, a Class checks whether the value is an instance, and a regular expression checks for a match. This is more flexible than treating case as a disguised chain of equality tests.
Iteration also has return-value semantics. each is for side effects and returns its receiver. map returns one transformed item for each input. select keeps matching items, while find stops at the first match. Picking the right iterator communicates intent and often removes bookkeeping variables.
Inside a loop or iterator, next skips the rest of the current iteration and break stops the nearest loop. redo starts the same iteration again without advancing, so it must be guarded carefully to avoid an endless loop. A manual while remains useful when progress depends on changing state rather than traversing a known collection.
Code Example
score = 86
grade = if score >= 90
'A'
elsif score >= 80
'B'
else
'C'
end
kind = case score
when 90..100 then :excellent
when 60..89 then :pass
else :retry
end
prices = [10, 25, 40]
discounted = prices.map { |price| price * 0.9 }
large = prices.select { |price| price >= 25 }
[grade, kind, discounted, large]
Common Mistakes & Pitfalls
Using a while counter when each/times would do (un-idiomatic), or confusing unless with if (unless = if not).
Check your understanding
Answer these without looking back at the article.
- When would you use Conditionals Loops in a real project?
- How would you compare Conditionals Loops with an alternative?
- What is a common mistake when using Conditionals Loops?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.