If - Else
Part of the Fundamentals section of Coddy's Ruby journey — lesson 22 of 88.
Sometimes you need your program to choose between two different paths. The else keyword lets you specify what should happen when the if condition is false:
age = 15
if age >= 18
puts "You can vote!"
else
puts "You're too young to vote."
endIn this example, since age is 15, the condition age >= 18 is false, so Ruby skips the first block and runs the code after else instead. The program will output "You're too young to vote."
With if-else, exactly one of the two blocks will always run. If the condition is true, the first block executes. If it's false, the second block executes:
balance = 100
withdrawal = 150
if balance >= withdrawal
puts "Withdrawal successful"
else
puts "Insufficient funds"
endThis structure is perfect when you have two mutually exclusive outcomes. The program must go one way or the other, never both and never neither.
Challenge
EasyYou are provided with the following variables:
temperature = 35
has_umbrella = false
account_balance = 50
withdrawal_amount = 75
player_score = 100
high_score = 100Use if-else statements to evaluate each scenario below. Print the appropriate message based on whether the condition is true or false.
- If
temperatureis greater than30, printIt's hot outside. Otherwise, printThe weather is nice. - If
has_umbrellaistrue, printYou're prepared for rain. Otherwise, printYou might get wet. - If
account_balanceis greater than or equal towithdrawal_amount, printWithdrawal approved. Otherwise, printInsufficient funds. - If
player_scoreis greater thanhigh_score, printNew high score!. Otherwise, printTry again. - If
temperatureis less than0ortemperatureis greater than40, printExtreme weather. Otherwise, printNormal conditions.
Each message should appear on its own line.
Cheat sheet
The else keyword specifies what should happen when the if condition is false:
age = 15
if age >= 18
puts "You can vote!"
else
puts "You're too young to vote."
endWith if-else, exactly one of the two blocks will always run. If the condition is true, the first block executes. If it's false, the second block executes:
balance = 100
withdrawal = 150
if balance >= withdrawal
puts "Withdrawal successful"
else
puts "Insufficient funds"
endTry it yourself
# Variables are already defined for you
temperature = 35
has_umbrella = false
account_balance = 50
withdrawal_amount = 75
player_score = 100
high_score = 100
# TODO: Write your code below
# Use if-else statements to evaluate each scenario and print the appropriate 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