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
We've learned about comparison operators, so we can test whether the user wants to order 50 or more widgets, or 100 or more. Now we just need to add code that applies a discounted price if those conditions are true.
- Like most programming languages, Ruby has an
if
statement that executes some code only if a condition expression is true. - Starts with
if
keyword - followed by condition, which in these examples are the boolean values
true
andfalse
- followed by one or more lines of code (like a method body, these are usually indented to make them easier to read, although it's not required)
- followed by
end
keyword - Because condition for first
if
alwaystrue
, code it contains will always be run. - Because condition for first
if
alwaysfalse
, code it contains will never be run.
if true # This will always be run
puts "true"
puts "additional code here"
end
if false # This will never be run
puts "false"
puts "additional code here"
end
- The output shows that the code in the first
if
statement ran, but the code in the secondif
statement didn't:
true
additional code here
- Actual programs will use expressions that evaluate to
true
orfalse
, like comparison operators.
if 75 > 50
puts "75 > 50"
end
if 75 > 100
puts "75 > 100"
end
if 50 == 50
puts "50 == 50"
end
Output:
75 > 50
50 == 50
- Actual programs aren't likely to compare hard-coded numbers, either. Usually, they'll test the value of a variable.
number = 75
if number > 50
puts "number > 50"
end
if number > 100
puts "number > 100"
end
if number == 50
puts "number == 50"
end
Output:
number > 50
-
if
runs some code only if a condition is true. What if you need to run some code only if a condition is false? - Many languages require you to use some ugly code to make this work with an
if
statement - But Ruby provides a second keyword,
unless
, that's basically the opposite ofif
. -
unless
runs code only if its condition is false.
number = 75
unless number > 50
puts "number > 50"
end
unless number > 100
puts "number > 100"
end
unless number == 50
puts "number == 50"
end
Output:
number > 100
number == 50
Widget store
- Let's try using
if
in our program:
# This is the same as before
def ask(question)
print question + " "
gets.chomp
end
# Updated to give discounts based on quantity
def price(quantity)
if quantity >= 100
price_per_unit = 8
end
if quantity >= 50
price_per_unit = 9
end
if quantity < 50
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}"
This works fine if the quantity is 99 or less, but for quantities of 100 or more, price_per_unit
gets set to 9
because 100
is also >= 50
. We'll fix that next.
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