This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
If you have some code that you want to run when an "if" statement doesn't run, you can add "else" or "elsif" clauses to it.
- If the condition of an
if
statement is false, the code in its body won't run.
if_condition = false
if if_condition
puts "if clause"
end
- If you have some code that you want to run when the
if
statement doesn't run, you can add anelse
clause. - You add the keyword
else
betweenif
andend
, followed by one or more lines of code that you want to run instead of theif
statement's code. - Ruby doesn't require it, but it's conventional to have the
else
keyword aligned with theif
keyword, and theelse
code indented one level. Again, it just makes the code easier to read.
if_condition = false
if if_condition
puts "if clause"
else
puts "else clause"
end
- If you have other conditions that you want to test only when the
if
condition is false, you can add one or moreelsif
clauses between theif
andelse
clauses. - You add the keyword
elsif
, another condition expression, and some code you want to run if the condition is true. - As with the
else
clause, theelsif
keyword should be lined up with theif
keyword, and the code following it should be indented one level.
if_condition = false
elsif_condition = true
if if_condition
puts "if clause"
elsif elsif_condition
puts "elsif clause"
else
puts "else clause"
end
- If the
if
clause istrue
, then neither theelsif
clauses nor theelse
clause will be run. - If the
if
condition and allelsif
conditions are false, then the else clause will be run. - The
else
clause isn't required. If all conditions are false and there's noelse
clause, then no code will be run.
Widget store
Let's apply what we've learned to the widget store program. Previously, we were checking the conditions for all three if
statements. We've tried to fix it so this won't happen, but if more than one condition were true, the price_per_unit
variable would be set and then overwritten. No matter what, we only want price_per_unit
set once. Let's use elsif
and else
clauses to clean this up.
# This is the same as before
def ask(question)
print question + " "
gets.chomp
end
def price(quantity)
# Merge the three old "if" statements
# into one if/elsif/else statement
if quantity >= 100
price_per_unit = 8
elsif quantity >= 50 && quantity < 100
price_per_unit = 9
else
price_per_unit = 10
end
quantity * price_per_unit
end
# This code is unchanged
puts "Welcome to the widget store!"
answer = ask("How many widgets are you ordering?")
number = answer.to_i
total = price(number)
puts "For #{number} widgets, your total is: $#{total}"
- But really, the
elsif
branch can't possibly run if the quantity is equal to or greater than 100, because then only theif
branch will run. So we can get rid of&& quantity < 100
.
def price(quantity)
if quantity >= 100
price_per_unit = 8
# Get rid of && operator:
elsif quantity >= 50
price_per_unit = 9
else
price_per_unit = 10
end
quantity * price_per_unit
end
After that, our bulk discount feature works great! Thanks to elsif
and else
clauses, we can be sure that a discount is applied only once.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up