**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 returns`true`

if the boolean value to its left*or*the boolean value to its right are true. - The "and" (
`&&`

) operator returns`true`

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`

becomes`false`

and`false`

becomes`true`

.

```
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.

If a user is ordering 100 or 0:00 more widgets, we set the price per unit variable to 8 as we should. 0:02 But then we overwrite price per unit with a value 0:07 of 9 because a quantity of 100 is also greater than the quantity of 50. 0:10 We need to test whether quantity is greater than or equal to 50 and 0:15 less than 100 before we apply a price per unit of 9. 0:19 We can do that using boolean operators. 0:23 [SOUND] Just like math operators return a new number based on other numbers, 0:26 boolean operators return a new boolean value based on other boolean values. 0:31 The Or operator returns true as the boolean value to its left or 0:37 the boolean value to its right are true. 0:41 The And operator returns true if the Boolean value to it's left and 0:44 the Boolean value to it's right are true. 0:49 And the Not operator returns the opposite of the Boolean value to its right, 0:51 that is true becomes false and false becomes true. 0:56 Let's launch IRB to experiment with some Boolean operators. 1:00 Here is how Boolean values appear when you type them in IRB. 1:04 If you type the value true, the result is going to be true. 1:07 If you type the value false, the result is going to be false. 1:10 Now, let's try changing those up, using Boolean operators. 1:14 We'll start with the Or operator. 1:17 So, if you type the Boolean value true and then use the Or 1:19 operator to combine that with the value false. 1:24 The result is going to be true, because it tests whether either the first value or 1:28 the second value is true. 1:32 And if either of them is true, the final result is true. 1:34 You get the same result if you try typing false or true. 1:38 Well, the second value is true, therefore the result of the Or operator is true. 1:43 The same holds if both values are true. 1:47 If we type true or true, the result is true. 1:50 But if both values on both sides are false, 1:53 then the result of the Or operator is going to be false. 1:58 Now once again, you're probably not going to be using Boolean operators with 2:01 hard-coded Boolean values too often. 2:05 You're probably going to be generating your Boolean values through comparison 2:07 operators. 2:11 So let's try the results of a couple of comparisons. 2:12 We'll test whether 2 is < 3 or 4 is > 5. 2:15 Well the first comparison is true and 2:19 therefore the result of the Or operator is true. 2:22 Now let's try it with 2:26 2 > 3 or 4 < 5. 2:31 Well 2 isn't greater than 3, so the first boolean value is false. 2:35 But 4 is less than 5, so the second value is true, and therefore the result is true. 2:40 Finally, let's try it with a test whether 2 is greater than 3, 2:45 or 4 is greater than 5. 2:50 Neither of those is true, and therefore the result of the Or operator is false. 2:53 And again, you're probably not going to be working with hard-coded 2:58 numbers very often either. 3:01 You're probably going to be working with values in variables. 3:03 Let's set a quantity variable that's equal to 75. 3:06 If we were to test whether quantity is less than 50, that would be false. 3:09 If we were to test whether quantity is greater than 100, 3:16 that would also be false. 3:20 Because quantity is currently set to 75. 3:22 And if we combine those two comparisons with an Or 3:25 operator, quantity less than 50 or 3:30 quantity, Greater than 100. 3:33 That too is going to be false because both of the Boolean values are false. 3:39 If however we change quantity to 25 and 3:44 then run the exact same comparison again. 3:48 This time it's true, 25 is less than 50. 3:52 Therefore, the first Boolean value is true. 3:55 And therefore the result of the Or operator is true. 3:58 If we change quantity, To 125, 4:01 and run the exact same comparison, that too is true. 4:06 The first Boolean value is false, but the second is true. 4:11 Therefore the results of the Or operator is true. 4:14 Now let's try the Boolean And operator. 4:17 Boolean And requires that the value on both sides be true in order for 4:19 it to return true. 4:24 If either is false, the And operator is going to return false. 4:25 So if we were to type true and 4:29 false, the result would be false because the second value is false. 4:32 Both values have to be true for And to return true. 4:36 Likewise, if the first value is false and 4:40 the second value is true, that's also going to be false. 4:43 If both values are false, That's also a false result. 4:47 Only if both values are true, Will you get a result of true from the And operator. 4:54 This of course works with comparisons as well. 5:04 If we test whether 2 is less than 3 which is true, and 5:06 4 is greater than 5 which is false. 5:10 Well the result is going to be false. 5:13 However if we test whether 2 is less than 3, and 4 is less than 5. 5:15 Both of those are true and therefore the result is true. 5:22 Again, the most common scenario for using the And Boolean operator, 5:26 is going to be when you are working with variables. 5:30 So, let's set our quantity variable equal to 25. 5:32 And let's test whether quantity is greater 5:36 than 50 and quantity is less than 100. 5:42 In this case the result of the first comparison is false and 5:47 so the result of the And operator is going to be false. 5:51 If we change quantity to 125. 5:55 Well, 125 is greater than 100. 5:59 So if we try the exact same comparison again, 6:02 that too will be false, because the second Boolean value is false. 6:05 It's only if both Boolean values are true 6:10 that we'll get a true result from the And operator. 6:15 So if we were to set quantity to 75. 6:17 75 is greater than 50 and it's less than 100, so 6:21 we finally get a true value for the And Boolean operator. 6:25 And finally, 6:29 we have the Not operator which we're not going to use in this program. 6:30 So we won't talk about it too extensively, but 6:33 let's just demonstrate it really quickly. 6:36 The Not operator takes the Boolean value to its right and reverses it. 6:38 So not true equals false. 6:42 And not false equals true. 6:46 If you want to know more about Boolean operators, we'll have further reading for 6:50 you in the teacher's notes. 6:54 But for now, let's try using Boolean operators to fix our program. 6:56 So here's the situation. 7:00 If our user is ordering 100 or more widgets, 7:01 the price per unit gets set to 8 as it's supposed to be. 7:04 But then, this if condition also runs. 7:07 Because any value that's 100 or over is also going to be greater than 50. 7:11 So this code here runs as well. 7:16 Overwriting the price-per-unit with a value of 9. 7:18 We need this code to run only if the quantity is greater than or equal to 50, 7:22 and if the quantity is less than 100. 7:27 So we've used the Boolean And operator here, to try to ensure that 7:34 the price per unit only gets set to 9 if the value is between 50 and 99. 7:39 So let's save this and try running it. 7:45 ruby widgets.rb. 7:48 And we'll try entering the value that was giving us trouble, 100. 7:51 It looks like that worked. 7:55 We entered a value of 100 or more and it was multiplied by 8. 7:57 Let's try a value greater than 100. 8:02 That seems to work as well. 8:05 It was multiplied by 8 again. 8:06 Now, let's test whether it still works for values of 50 or more. 8:08 So how many widgets are you ordering? 8:13 50. 8:15 We entered 50 widgets, and this time, it was multiplied by 9. 8:16 So that discount seems to be working, as well. 8:19 Let's try 51, and it was multiplied by 9 again. 8:22 Now, let's try 99. 8:27 99 is also less than 100, so it was multiplied by 9 again. 8:29 And just in case, 8:34 let's make sure that orders of less than 50 units are working as well. 8:35 Let's try ordering 10. 8:38 And that was multiplied by full price, $10 per widget. 8:40 So everything's working. 8:45 But really, this solution is kind of sloppy. 8:46 If we ever make a change to this logic, we could find ourselves in a situation where 8:49 one price per unit value is being overwritten by a different value again. 8:53 We only want the code in each of these if statements to run when 8:58 none of the others is true. 9:01 Next, we'll look at a way to do that. 9:03

You need to sign up for Treehouse in order to download course files.

Sign up