Locked lesson.
About this lesson
How to use if/else/elsif statements in Ruby.
Exercise files
Download this lesson’s related exercise files.
Conditional Statements.docx58.8 KB Conditional Statements - Solution.docx
59.1 KB
Quick reference
Conditional Statements
Conditional Statements allow you to compare things and make decisions based on the outcomes.
When to use
Use these whenever you need to choose between two or more things.
Instructions
A basic if statement looks like this:
if num > 10
puts "Your number is greater than 10"
end
A basic IF/ELSE Statement looks like this:
if num > 10
puts "Your number is greater than 10"
else
puts "Your number is not greater than 10"
end
A basic IF/ELSIF Statement looks like this:
if num > 10
puts "Your number is greater than 10"
elsif num == 9
puts "Your number is 9!"
else
puts "Your number is not greater than 10"
end
Hints & tips
- If/Else statements let you make decisions in your program
- Use your comparison operators in your conditional statements
Lesson notes are only available for subscribers.