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
Just like math operators return a new number based on other numbers, boolean operators return a new boolean value based on other boolean values.
Just like math operators return a new number based on other numbers, boolean operators return a new boolean value based on other boolean values.
- The "or" (
||
) operator returnstrue
if the boolean value to its left or the boolean value to its right are true. - The "and" (
&&
) operator returnstrue
if the boolean value to its left and the boolean value to its right are true. - And the "not" (
!
) operator returns the opposite of the boolean value to its right. That is,true
becomesfalse
andfalse
becomestrue
.
true || false
true && false
!false
||
$ irb
2.3.0 :001 > true
=> true
2.3.0 :002 > false
=> false
2.3.0 :003 > true || false
=> true
2.3.0 :004 > false || true
=> true
2.3.0 :005 > true || true
=> true
2.3.0 :005 > false || false
=> false
2.3.0 :006 > 2 < 3 || 4 > 5
=> true
2.3.0 :007 > 2 > 3 || 4 < 5
=> true
2.3.0 :008 > 2 > 3 || 4 > 5
=> false
2.3.0 :009 > quantity = 75
=> 75
2.3.0 :011 > quantity < 50
=> false
2.3.0 :011 > quantity > 100
=> false
2.3.0 :011 > quantity < 50 || quantity > 100
=> false
2.3.0 :012 > quantity = 25
=> 25
2.3.0 :013 > quantity < 50 || quantity > 100
=> true
2.3.0 :014 > quantity = 125
=> 125
2.3.0 :015 > quantity < 50 || quantity > 100
=> true
&&
2.3.0 :016 > true && false
=> false
2.3.0 :017 > false && true
=> false
2.3.0 :018 > false && false
=> false
2.3.0 :019 > true && true
=> true
2.3.0 :020 > 2 < 3 && 4 > 5
=> false
2.3.0 :021 > 2 < 3 && 4 < 5
=> true
2.3.0 :023 > quantity = 25
=> 25
2.3.0 :024 > quantity > 50 && quantity < 100
=> false
2.3.0 :025 > quantity = 125
=> 125
2.3.0 :026 > quantity > 50 && quantity < 100
=> false
2.3.0 :027 > quantity = 75
=> 75
2.3.0 :028 > quantity > 50 && quantity < 100
=> true
!
2.3.0 :029 > !true
=> false
2.3.0 :030 > !false
=> true
Widget store
- Let's try using boolean operators to fix our program:
# This is the same as before
def ask(question)
print question + " "
gets.chomp
end
def price(quantity)
if quantity >= 100
price_per_unit = 8
end
# Add "&& quantity < 100" here
if quantity >= 50 && quantity < 100
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}"
But really, this solution is kind of sloppy. If we ever make a change to this logic, we could find ourselves in a situation where one price_per_unit
value is being overwritten by another again. We only want the code in each of these if
statements to run when none of the others is true. Next, we'll look at a way to do that.
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