Ruby if...else

Make decisions in your Ruby programs

🔀 What is if...else in Ruby?

The if...else statement allows your program to make decisions based on conditions. It executes different code blocks depending on whether a condition is true or false, enabling dynamic and responsive program behavior.


age = 18

if age >= 18
  puts "You are an adult"
else
  puts "You are a minor"
end
# Output: You are an adult
                                    

Output:

You are an adult

Conditional Concepts

if Statement

Execute code if condition is true

if score > 50
  puts "Pass"
end

else Statement

Execute code if condition is false

if score > 50
  puts "Pass"
else
  puts "Fail"
end
🔄

elsif Statement

Check multiple conditions

if score > 90
  puts "A"
elsif score > 80
  puts "B"
end
🚫

unless Statement

Opposite of if statement

unless tired
  puts "Keep going"
end

🔹 Basic if Statement

The if statement executes a block of code only when a condition evaluates to true. If the condition is false, the code inside the if block is skipped. This is the foundation of conditional logic in Ruby programming.

# Simple if statement
temperature = 30

if temperature > 25
  puts "It's hot outside!"
end
# Output: It's hot outside!

# If with comparison
score = 85

if score >= 60
  puts "You passed the exam"
end
# Output: You passed the exam

# If statement with boolean
is_raining = true

if is_raining
  puts "Take an umbrella"
end
# Output: Take an umbrella
                            

Output:

It's hot outside!

You passed the exam

Take an umbrella

🔹 if...else Statement

The else clause provides an alternative code block that executes when the if condition is false. This ensures that one of the two code paths will always execute, making your program handle both true and false scenarios effectively.

# if...else example
age = 15

if age >= 18
  puts "You can vote"
else
  puts "You cannot vote yet"
end
# Output: You cannot vote yet

# Another example
number = 7

if number % 2 == 0
  puts "#{number} is even"
else
  puts "#{number} is odd"
end
# Output: 7 is odd

# With user status
is_member = false

if is_member
  puts "Welcome back, member!"
else
  puts "Please sign up"
end
# Output: Please sign up
                            

Output:

You cannot vote yet

7 is odd

Please sign up

🔹 elsif for Multiple Conditions

Use elsif to check multiple conditions in sequence. Ruby evaluates each condition from top to bottom and executes the first matching block. This is perfect for categorizing values or handling multiple scenarios without nested if statements.

# Grade calculator
score = 85

if score >= 90
  puts "Grade: A"
elsif score >= 80
  puts "Grade: B"
elsif score >= 70
  puts "Grade: C"
elsif score >= 60
  puts "Grade: D"
else
  puts "Grade: F"
end
# Output: Grade: B

# Temperature check
temp = 15

if temp > 30
  puts "Very hot"
elsif temp > 20
  puts "Warm"
elsif temp > 10
  puts "Cool"
else
  puts "Cold"
end
# Output: Cool
                            

Output:

Grade: B

Cool

🔹 unless Statement

The unless statement is the opposite of if - it executes code when a condition is false. This makes code more readable when you want to express negative conditions naturally. Think of unless as "if not" in plain English.

# unless example
is_weekend = false

unless is_weekend
  puts "Go to work"
end
# Output: Go to work

# unless with else
has_ticket = false

unless has_ticket
  puts "Please buy a ticket"
else
  puts "Enjoy the show"
end
# Output: Please buy a ticket

# Practical example
account_balance = 50

unless account_balance > 100
  puts "Low balance warning"
end
# Output: Low balance warning
                            

Output:

Go to work

Please buy a ticket

Low balance warning

🔹 Inline if and unless

Ruby allows you to write if and unless statements on a single line, called modifiers. Place the condition after the statement for concise code. This style is perfect for simple conditions and makes your code more expressive and Ruby-like.

# Inline if (modifier form)
age = 20
puts "Adult" if age >= 18
# Output: Adult

# Inline unless
is_busy = false
puts "Let's go!" unless is_busy
# Output: Let's go!

# With variables
score = 95
bonus = 10 if score > 90
puts "Bonus: #{bonus}"
# Output: Bonus: 10

# Multiple examples
temperature = 35
puts "Stay hydrated" if temperature > 30
# Output: Stay hydrated

logged_in = true
puts "Access granted" if logged_in
# Output: Access granted
                            

Output:

Adult

Let's go!

Bonus: 10

Stay hydrated

Access granted

🔹 Comparison Operators

Comparison operators evaluate relationships between values and return true or false. Common operators include == (equal), != (not equal), > (greater than), < (less than), >= (greater or equal), and <= (less or equal). These form the conditions in your if statements.

# Equal to
x = 5
if x == 5
  puts "x is 5"
end
# Output: x is 5

# Not equal to
name = "Alice"
if name != "Bob"
  puts "Not Bob"
end
# Output: Not Bob

# Greater than and less than
age = 25
if age > 18 && age < 65
  puts "Working age"
end
# Output: Working age

# Greater or equal, less or equal
score = 60
if score >= 60
  puts "Pass"
end
# Output: Pass
                            

Output:

x is 5

Not Bob

Working age

Pass

🔹 Logical Operators

Combine multiple conditions using logical operators: && (and), || (or), and ! (not). The && operator requires all conditions to be true, || requires at least one to be true, and ! negates a condition. These create complex decision logic.

# AND operator (&&)
age = 25
has_license = true

if age >= 18 && has_license
  puts "Can drive"
end
# Output: Can drive

# OR operator (||)
day = "Saturday"

if day == "Saturday" || day == "Sunday"
  puts "It's the weekend!"
end
# Output: It's the weekend!

# NOT operator (!)
is_raining = false

if !is_raining
  puts "No umbrella needed"
end
# Output: No umbrella needed

# Combined operators
score = 85
attendance = 90

if score >= 80 && attendance >= 85
  puts "Excellent performance"
end
# Output: Excellent performance
                            

Output:

Can drive

It's the weekend!

No umbrella needed

Excellent performance

🧠 Test Your Knowledge

What does the unless statement do?