Home » Tutorial: if…else Statements in Ruby

Tutorial: if…else Statements in Ruby

Control flow in Ruby is managed through conditional statements like if…else, which execute code based on whether conditions are true or false.

Ruby’s syntax for conditional statements is clean and intuitive.

What You’ll Learn

1. Introduction to if…else

The if statement evaluates a condition. If the condition is true, the associated block of code executes. If the condition is false, the else block (if present) executes.

2. Basic if Statement

Syntax

if condition
  # Code to execute if condition is true
end

Example

x = 10
if x > 5
  puts "x is greater than 5"
end
# Output: x is greater than 5

3. if…else Statement

The else block runs if the if condition is false.

Syntax

if condition
  # Code if condition is true
else
  # Code if condition is false
end

Example

x = 3
if x > 5
  puts "x is greater than 5"
else
  puts "x is not greater than 5"
end
# Output: x is not greater than 5

4. elsif for Multiple Conditions

Use elsif to handle multiple conditions.

Syntax

if condition1
  # Code if condition1 is true
elsif condition2
  # Code if condition2 is true
else
  # Code if none of the above conditions are true
end

Example

x = 15
if x < 10
  puts "x is less than 10"
elsif x == 15
  puts "x is equal to 15"
else
  puts "x is greater than 10 but not 15"
end
# Output: x is equal to 15

5. Single-Line if Statements

For simple conditions, Ruby allows single-line if statements.

Syntax

puts "x is positive" if x > 0

Example

x = 7
puts "x is greater than 5" if x > 5
# Output: x is greater than 5

6. unless Statements

The unless statement is the opposite of if. It executes code if the condition is false.

Syntax

unless condition
  # Code to execute if condition is false
end

Example

x = 3
unless x > 5
  puts "x is not greater than 5"
end
# Output: x is not greater than 5

Single-Line unless

x = 10
puts "x is not less than 5" unless x < 5
# Output: x is not less than 5

7. Ternary Conditional Operator

The ternary operator is a concise alternative to if…else.

Syntax

condition ? true_value : false_value

Example

x = 10
result = x > 5 ? "greater than 5" : "5 or less"
puts result
# Output: greater than 5

8. Practical Examples

8.1 Check Even or Odd

number = 4
if number.even?
  puts "The number is even"
else
  puts "The number is odd"
end
# Output: The number is even

8.2 Determine Grade

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

8.3 User Authentication

username = "admin"
password = "password123"

if username == "admin" && password == "password123"
  puts "Login successful"
else
  puts "Invalid credentials"
end
# Output: Login successful

8.4 Categorize Age Group

age = 25
if age < 13
  puts "Child"
elsif age >= 13 && age < 18
  puts "Teenager"
elsif age >= 18 && age < 65
  puts "Adult"
else
  puts "Senior"
end
# Output: Adult

8.5 Default Value with ||

user_name = nil
puts user_name || "Guest"
# Output: Guest

8.6 Single-Line Guard Clause

age = 20
puts "You are eligible to vote" if age >= 18
# Output: You are eligible to vote

8.7 Check File Existence

file_path = "example.txt"
if File.exist?(file_path)
  puts "The file exists"
else
  puts "The file does not exist"
end

9. Summary

Key Constructs

  • if…else: Standard conditional branching.
  • elsif: Handle multiple conditions.
  • unless: Alternative for checking false conditions.
  • Ternary Operator: Concise alternative to if…else.

Best Practices

  1. Use single-line if for simple conditions to enhance readability.
  2. Avoid deep nesting of conditions; consider refactoring complex logic.
  3. Use unless sparingly, as it can sometimes be harder to read.

Mastering if…else constructs enables you to write clean and logical Ruby programs that handle conditional logic effectively

You may also like