The Ruby if...else
statement is used to execute/skip a block of code based on whether a condition is true
or false
.
Here's a simple example of the if...else
statement. You can read the rest of the tutorial to learn more.
Example
num = 35
# Check if num is a positive number
if num > 0
puts "Positive number"
# Else, check if num is negative
elsif num < 0
puts "Negative number"
# If both conditions fail, num is 0
else
puts "Zero"
end
# Output: Positive number
In the above example, the program displays
Positive number
if num is greater than 0.Negative number
if num is less than 0.Zero
if none of the conditions match.
Ruby if Statement
We use the if
keyword to execute code based on some specific condition.
The syntax of the if
statement is:
if condition
# Block of code
end
The if
keyword checks the condition
and chooses which code to execute based on the result. If the condition
is evaluated to:
true
- The code insideif
is executed.false
- The code insideif
is skipped.
Notice the end
keyword in the syntax; it is used to indicate the end of the if
statement.
Remember: Forgetting to use the end
keyword will result in an error.

Example 1: Ruby if Statement
# Program to check if the student passed
score = 96
# Check if score is greater than or equal to 40
if score >= 40
# The body of the if statement
puts "You passed the examination."
end
puts "Program executed!"
Output
You passed the examination. Program executed!
In the above program, the condition score >= 40
evaluates to true
since the score is 96. Thus, the body of the if
statement is executed.
Had the value of the score been lower than 40 (say, 35), then the body of the if
statement wouldn't have been executed.
Finally, since puts "Program executed!"
is outside the body of the if
statement, it is executed regardless of the results of the if
statement.
Tip: Try changing the value of score
to something less than 40 and see what happens.
One-line if Statement
Ruby also allows you to use a one-line if statement with the following syntax:
code if condition
Here, code
will be executed if the condition
is true
. For example,
score = 96
puts "You passed!" if score >= 40
# Output: You passed!
Here, the code puts "You passed!"
is executed because the condition score >= 40
is true
.
Ruby else Statement
The else
keyword executes a block of code when the condition in the preceding if
statement evaluates to false
.
Note: The else
statement should always follow an if
statement. In other words, the if
and else
statements are parts of a single conditional structure.
The syntax of the if...else
statement is:
if condition
# Block of code to execute if the condition is true
else
# Block of code to execute if the condition is false
end
The if...else
statement checks condition
and executes code in two ways. If condition
is:
true
- The code insideif
is executed. And, the code insideelse
is skipped.false
- The code insideif
is skipped. Instead, the code insideelse
is executed.

Example 2: Ruby if…else Statement
# Program to check if the student passed or failed
score = 35
# Check if score is greater than or equal to 40
if score >= 40
# The body of the if statement
puts "You passed the examination."
else
# The body of the else statement
puts "You failed the examination."
end
puts "Program executed!"
Output
You failed the examination. Program executed!
Since score is 35, the if
condition (score >= 40
) evaluates to false
.
Thus, the code inside if
is skipped. And, the code inside else
is executed.
Ruby elsif Statement
The elsif
keyword is used to check for additional conditions if the initial if
statement is false
.
Notes:
- In Ruby,
elsif
stands for "else if." - An
if...elsif...else
statement is also known as an if...else ladder.
The syntax of the elsif
statement is:
# Check for first condition
if condition1
# if body
# Check for second condition
elsif condition2
# elsif body
# If no condition matches
else
# else body
end
Here:
- First, the condition in the
if
statement is checked. If the condition evaluates totrue
, the body ofif
is executed, and the rest is skipped. - Otherwise, the condition in the
elsif
statement is checked. Iftrue
, its body is executed and the rest is skipped. - Finally, if no condition matches, the block of code in
else
is executed.

Example 3: Ruby if...elsif Statement
score = 59
# Check if score is 80 or above
if score >= 80
puts "Excellent!"
# Else, check if score is 40 or above
elsif score >= 40
puts "Average"
# If both conditions fail, you fail the exam
else
puts "Failure!"
end
# Output: Average
Here, the if
condition is false
because score is 59. However, the elsif
condition is satisfied, so the program prints Average
.
Frequently Asked Question
We can use the elsif
keyword as many times as we want. For example,
score = 85
# Condition for passing with second division
if score >= 40 && score < 60
puts "Second division"
# Condition for passing with first division
elsif score >= 60 && score < 80
puts "First division"
# Condition for passing with distinction
elsif score >= 80 && score <= 100
puts "Distinction"
# Condition for failing the exam
elsif score > 0 && score < 40
puts "You failed the examination."
# If all conditions fail, the score is invalid
else
puts "Invalid score!"
end
# Output: Distinction
In the above example, we used three elsif
statements.
The second elsif
statement is executed as its condition is satisfied, while the conditions of the if
and the first elsif
statements are not satisfied.
Nested if...else Statement
When we use an if...else
statement inside another if...else
statement, we create a nested if...else statement. For example,
score = 60
# Outer if...else statement
# Student passed if score 40 or above
# Otherwise, student failed
if score >= 40
# Inner if...else statement
# Distinction if score is 80 or above
if score >= 80
puts "Distinction"
else
puts "Passed"
end
else
puts "Failed"
end
# Output: Passed
Outer if...else Statement
In the above example, the outer if
condition checks if a student has passed or failed using the condition score >= 40
. If it evaluates to false
, the outer else
statement will print "Failed"
.
On the other hand, if score >= 40
evaluates to true
, the program moves to the inner if...else
statement.
Inner if...else Statement
The inner if
condition checks whether the student passed with distinction using the condition score >= 80
.
If score >= 80
evaluates to true
, the inner if
statement will print "Distinction"
.
Otherwise, the inner else
statement will print "Passed"
.
Note: Avoid nesting multiple if...else
statements within each other to maintain code readability and simplify debugging.
More on Ruby if...else Statement
Non-boolean values that are considered true by Ruby are known as truthy, while those considered false are known as falsy.
Ruby treats the following values as false
:
false
nil
Everything else is considered true
, including:
- 0
- Empty strings (
""
) - Empty arrays (
[]
)
To learn more, visit Ruby Boolean.
We can use the ternary operator ?:
instead of an if...else
statement if our operation is very simple. For example,
score = 40
result = (score >= 40) ? "pass" : "fail"
puts result
# Output: pass
We can replace our if…else
statement with the case statement when we deal with a large number of conditions. For example,
score = "C"
# Using case statement
case score
# First condition
when "A"
puts "Excellent!"
# Second condition
when "B"
puts "Good!"
# Third condition
when "C"
puts "Average"
# Fourth condition
when "D"
puts "Bad"
else
puts "Fail"
end
# Output: Average
As you can see, the case
statement makes our code more readable and maintainable.
We can use logical operators such as &&
and ||
within an if
statement to add multiple conditions. For example,
age = 35
salary = 6000
# Combine two conditions using the "and" operator &&
if age >= 30 && salary >= 5000
puts "Eligible for premium membership."
else
puts "Not eligible for premium membership."
end
# Output: Eligible for premium membership.
Here, we used the logical operator &&
to add two conditions in the if
statement. The two conditions are:
age >= 30
salary >= 5000
Because of the &&
operator, both conditions must be true
for the if
block to be executed.
Yes, you can use user input inside your if...else
statement. Just make sure that you've converted the input to a suitable type. For example,
print "Enter your exam score: "
# Get user input using gets
user_input = gets.chomp
# Convert the input to integer
score = user_input.to_i
# Check if the user passed or failed
if score >= 40
puts "pass"
else
puts "fail"
end
Output 1
Enter your exam score: 56 pass
Output 2
Enter your exam score: 25 fail
Here, we used gets.chomp
to get user input, which is stored as a string in the user_input
variable.
So, we convert the input into an integer using the to_i
method, and then check if the user passed or failed.