Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic 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.

0:00
If a user is ordering 100 or

0:02
more widgets, we set the price per unit variable to 8 as we should.

0:07
But then we overwrite price per unit with a value

0:10
of 9 because a quantity of 100 is also greater than the quantity of 50.

0:15
We need to test whether quantity is greater than or equal to 50 and

0:19
less than 100 before we apply a price per unit of 9.

0:23
We can do that using boolean operators.

0:26
[SOUND] Just like math operators return a new number based on other numbers,

0:31
boolean operators return a new boolean value based on other boolean values.

0:37
The Or operator returns true as the boolean value to its left or

0:41
the boolean value to its right are true.

0:44
The And operator returns true if the Boolean value to it's left and

0:49
the Boolean value to it's right are true.

0:51
And the Not operator returns the opposite of the Boolean value to its right,

0:56
that is true becomes false and false becomes true.

1:00
Let's launch IRB to experiment with some Boolean operators.

1:04
Here is how Boolean values appear when you type them in IRB.

1:07
If you type the value true, the result is going to be true.

1:10
If you type the value false, the result is going to be false.

1:14
Now, let's try changing those up, using Boolean operators.

1:17
We'll start with the Or operator.

1:19
So, if you type the Boolean value true and then use the Or

1:24
operator to combine that with the value false.

1:28
The result is going to be true, because it tests whether either the first value or

1:32
the second value is true.

1:34
And if either of them is true, the final result is true.

1:38
You get the same result if you try typing false or true.

1:43
Well, the second value is true, therefore the result of the Or operator is true.

1:47
The same holds if both values are true.

1:50
If we type true or true, the result is true.

1:53
But if both values on both sides are false,

1:58
then the result of the Or operator is going to be false.

2:01
Now once again, you're probably not going to be using Boolean operators with

2:05
hardcoded Boolean values too often.

2:07
You're probably going to be generating your Boolean values through comparison

2:11
operators.

2:12
So let's try the results of a couple of comparisons.

2:15
We'll test whether 2 is < 3 or 4 is > 5.

2:19
Well the first comparison is true and

2:22
therefore the result of the Or operator is true.

2:26
Now let's try it with

2:31
2 > 3 or 4 < 5.

2:35
Well 2 isn't greater than 3, so the first boolean value is false.

2:40
But 4 is less than 5, so the second value is true, and therefore the result is true.

2:45
Finally, let's try it with a test whether 2 is greater than 3,

2:50
or 4 is greater than 5.

2:53
Neither of those is true, and therefore the result of the Or operator is false.

2:58
And again, you're probably not going to be working with hardcoded

3:01
numbers very often either.

3:03
You're probably going to be working with values in variables.

3:06
Let's set a quantity variable that's equal to 75.

3:09
If we were to test whether quantity is less than 50, that would be false.

3:16
If we were to test whether quantity is greater than 100,

3:20
that would also be false.

3:22
Because quantity is currently set to 75.

3:25
And if we combine those two comparisons with an Or

3:30
operator, quantity less than 50 or

3:33
quantity, Greater than 100.

3:39
That too is going to be false because both of the Boolean values are false.

3:44
If however we change quantity to 25 and

3:48
then run the exact same comparison again.

3:52
This time it's true, 25 is less than 50.

3:55
Therefore, the first Boolean value is true.

3:58
And therefore the result of the Or operator is true.

4:01
If we change quantity, To 125,

4:06
and run the exact same comparison, that too is true.

4:11
The first Boolean value is false, but the second is true.

4:14
Therefore the results of the Or operator is true.

4:17
Now let's try the Boolean And operator.

4:19
Boolean And requires that the value on both sides be true in order for

4:24
it to return true.

4:25
If either is false, the And operator is going to return false.

4:29
So if we were to type true and

4:32
false, the result would be false because the second value is false.

4:36
Both values have to be true for And to return true.

4:40
Likewise, if the first value is false and

4:43
the second value is true, that's also going to be false.

4:47
If both values are false, That's also a false result.

4:54
Only if both values are true, Will you get a result of true from the And operator.

5:04
This of course works with comparisons as well.

5:06
If we test whether 2 is less than 3 which is true, and

5:10
4 is greater than 5 which is false.

5:13
Well the result is going to be false.

5:15
However if we test whether 2 is less than 3, and 4 is less than 5.

5:22
Both of those are true and therefore the result is true.

5:26
Again, the most common scenario for using the And Boolean operator,

5:30
is going to be when you are working with variables.

5:32
So, let's set our quantity variable equal to 25.

5:36
And let's test whether quantity is greater

5:42
than 50 and quantity is less than 100.

5:47
In this case the result of the first comparison is false and

5:51
so the result of the And operator is going to be false.

5:55
If we change quantity to 125.

5:59
Well, 125 is greater than 100.

6:02
So if we try the exact same comparison again,

6:05
that too will be false, because the second Boolean value is false.

6:10
It's only if both Boolean values are true

6:15
that we'll get a true result from the And operator.

6:17
So if we were to set quantity to 75.

6:21
75 is greater than 50 and it's less than 100, so

6:25
we finally get a true value for the And Boolean operator.

6:29
And finally,

6:30
we have the Not operator which we're not going to use in this program.

6:33
So we won't talk about it too extensively, but

6:36
let's just demonstrate it really quickly.

6:38
The Not operator takes the Boolean value to its right and reverses it.

6:42
So not true equals false.

6:46
And not false equals true.

6:50
If you want to know more about Boolean operators, we'll have further reading for

6:54
you in the teacher's notes.

6:56
But for now, let's try using Boolean operators to fix our program.

7:00
So here's the situation.

7:01
If our user is ordering 100 or more widgets,

7:04
the price per unit gets set to 8 as it's supposed to be.

7:07
But then, this if condition also runs.

7:11
Because any value that's 100 or over is also going to be greater than 50.

7:16
So this code here runs as well.

7:18
Overwriting the priceperunit with a value of 9.

7:22
We need this code to run only if the quantity is greater than or equal to 50,

7:27
and if the quantity is less than 100.

7:34
So we've used the Boolean And operator here, to try to ensure that

7:39
the price per unit only gets set to 9 if the value is between 50 and 99.

7:45
So let's save this and try running it.

7:48
ruby widgets.rb.

7:51
And we'll try entering the value that was giving us trouble, 100.

7:55
It looks like that worked.

7:57
We entered a value of 100 or more and it was multiplied by 8.

8:02
Let's try a value greater than 100.

8:05
That seems to work as well.

8:06
It was multiplied by 8 again.

8:08
Now, let's test whether it still works for values of 50 or more.

8:13
So how many widgets are you ordering?

8:15
50.

8:16
We entered 50 widgets, and this time, it was multiplied by 9.

8:19
So that discount seems to be working, as well.

8:22
Let's try 51, and it was multiplied by 9 again.

8:27
Now, let's try 99.

8:29
99 is also less than 100, so it was multiplied by 9 again.

8:34
And just in case,

8:35
let's make sure that orders of less than 50 units are working as well.

8:38
Let's try ordering 10.

8:40
And that was multiplied by full price, $10 per widget.

8:45
So everything's working.

8:46
But really, this solution is kind of sloppy.

8:49
If we ever make a change to this logic, we could find ourselves in a situation where

8:53
one price per unit value is being overwritten by a different value again.

8:58
We only want the code in each of these if statements to run when

9:01
none of the others is true.

9:03
Next, we'll look at a way to do that.
You need to sign up for Treehouse in order to download course files.
Sign up