Thuta Learning
IntermediateProgrammingbeginner

If / Else / Elsif

Relax. We'll talk through this in plain words — no textbook voice.

if, elsif, else let you branch your code based on a condition. This is essential for things like checking a login, checking age, or checking whether something is in stock.

ruby
age = 20

if age < 18
  puts "You are a minor."
elsif age >= 18 && age < 65
  puts "You are an adult."
else
  puts "You are a senior."
end

Ruby checks each condition from top to bottom. As soon as it finds a block that's true, it runs that block and skips the rest.

You should see
You are an adult.

Info

&& means the result is true only if both conditions are true. || means the result is true if at least one condition is true.

Easy traps

  • You can't spell it elseif — in Ruby, the correct spelling is elsif.
If / Else / Elsif | Thuta Learning