Nested If - Else
Part of the Fundamentals section of Coddy's Ruby journey — lesson 24 of 88.
What if you need to check more than two conditions? You can place an if-else statement inside another if-else statement. This is called nesting, and it lets you handle multiple decision points.
score = 85
if score >= 90
puts "Grade: A"
else
if score >= 80
puts "Grade: B"
else
puts "Grade: C"
end
endRuby checks the first condition. If it's false, it moves to the else block where another if-else waits. This creates a chain of decisions, each narrowing down the possibilities.
Ruby provides a cleaner way to write this using elsif, which combines else and if into one keyword:
score = 85
if score >= 90
puts "Grade: A"
elsif score >= 80
puts "Grade: B"
else
puts "Grade: C"
endBoth examples produce the same result, but elsif keeps your code flatter and easier to read. You can chain as many elsif conditions as you need, and the final else catches anything that doesn't match the previous conditions.
Challenge
EasyYou are provided with the following variable:
temperature = 15Create a weather advisory system using if, elsif, and else to classify the temperature and print the appropriate message:
- If
temperatureis greater than or equal to30, printHot - Stay hydrated - If
temperatureis greater than or equal to20, printWarm - Enjoy the day - If
temperatureis greater than or equal to10, printCool - Bring a jacket - If
temperatureis greater than or equal to0, printCold - Bundle up - Otherwise, print
Freezing - Stay indoors
Output only the single message that matches the temperature.
Cheat sheet
You can nest if-else statements inside each other to check multiple conditions:
score = 85
if score >= 90
puts "Grade: A"
else
if score >= 80
puts "Grade: B"
else
puts "Grade: C"
end
endRuby provides elsif as a cleaner way to chain multiple conditions:
score = 85
if score >= 90
puts "Grade: A"
elsif score >= 80
puts "Grade: B"
else
puts "Grade: C"
endYou can chain as many elsif conditions as needed. The final else catches anything that doesn't match the previous conditions.
Try it yourself
temperature = 15
# TODO: Write your code below to classify the temperature
# Use if, elsif, and else to print the appropriate weather advisory messageThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Logical Operators Part 42Variables and Data Types
Numbers and VariablesString Data TypeBoolean Data TypeSymbol Data TypeChecking Data TypesNaming ConventionsRecap - Variable Creation8Loops
For Loop with RangesWhile LoopBreakNextRecap - FactorialTimes LoopUntil LoopNested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsRecap - Simple MathComparison Operators6Basic IO
Output with putsOutput with print and pOutput With VariablesInput with getsChomp MethodType ConversionRecap - Age CalculatorRecap - True or False