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
Continue learning the basics of the Logical Operators: AND, OR and NOT.
-
0:00
Okay, we're back here inside our boxOfficePlus project.
-
0:04
Since we've now determined if the customer qualifies for the age discount,
-
0:08
we'll wanna create our conditionals for assigning the different ticket prices.
-
0:12
Essentially, we'll test to see if conditions are met,
-
0:14
and if so, assign the correct ticket price.
-
0:17
By the way, we won't worry about doing this the most elegant way but rather we'll
-
0:21
concentrate on simply reviewing the syntax and usage of and, or, and not.
-
0:27
Speaking of syntax,
-
0:29
it seems like the perfect time to show you a useful bit of shorthand.
-
0:33
Often, you'll see a boolean checked, like this.
-
0:41
You can do that, but you can actually just do this, that says the same thing.
-
0:48
It checks to make sure the age discount is true.
-
0:52
Okay, that new shorthand should help keep things tidy.
-
0:56
First, let's test for customers who have the age discount,
-
1:00
are attending a matinee, and are not employees.
-
1:03
So we'll want to say if (ageDiscount
-
1:08
&& isMatinee && !isEmployee).
-
1:14
And if they are, then we'll wanna set the customerPrice,
-
1:22
we'll wanna set that equal to 6.50.
-
1:28
Alarm bells anyone?
-
1:31
That's right, we've got another magic number sitting right here.
-
1:35
Let's replace that with a variable that we're gonna call age and matinee price.
-
1:41
So we'll declare those right here, below all our price tiers.
-
1:46
So we're gonna wanna float for
-
1:49
regularPrice that'll equal $10.
-
1:54
Another float for
-
1:58
ageOrMatineePrice.
-
2:04
Another float for ageAndMatineePrice,
-
2:12
that's 6.50.
-
2:15
Another float for employeeRegPrice,
-
2:21
that equals 4.50.
-
2:25
And then lastly, we have the employeeMatineePrice,
-
2:31
which is the bargain basement, free movie.
-
2:37
Now of course, those names I came up with are just what I decided to use because I
-
2:41
think they're useful and descriptive.
-
2:43
We could change them to whatever we want, so
-
2:45
long as we have the five different price levels.
-
2:48
All right, now we can replace that magic number with our
-
2:52
ageAndMatineePrice.
-
2:58
Next, we'll create an if else to catch the cases where the customer qualifies for
-
3:03
one of the discounts and is not an employee.
-
3:06
So we'll say, else if, one parenthetical statement inside will say ageDiscount.
-
3:16
Or, that's the pipes, isMatinee.
-
3:21
And then outside of that parentheses, we'll say &&
-
3:27
!isEmployee.
-
3:32
And if that's the case, we're gonna say the customerPrice = ageOrMatineePrice.
-
3:40
Okay, now notice here, we needed to nest our conditions.
-
3:46
The reason is, first, we want to check to see if at least one
-
3:51
of these two conditions, they have the ageDiscount or isMatinee, is true.
-
3:56
If one or both of those return true,
-
3:58
then we're gonna check the and, that they are not an employee.
-
4:03
If that's true, we'll assign the age or matinee price.
-
4:07
By the way, that conditional statement on its own would return true for
-
4:12
someone with an age discount who is seeing a matinee and
-
4:16
is not an employee and it would overcharge them.
-
4:19
It should charge them this price.
-
4:21
However, because we used an else if, if they were to qualify for
-
4:25
an age discount, and a matinee, and are not an employee,
-
4:29
they would hit this curly brace and simply skip the rest of the else ifs.
-
4:34
So again, that's really important to notice when you're using ifs and else ifs.
-
4:38
Next, we'll wanna check for an employee attending a non-matinee.
-
4:42
So let's give ourselves some space here, and we'll use another else if,
-
4:49
and we'll say, is it an employee and is it not a matinee?
-
4:56
If that's the case, then we need to assign customer price is
-
5:02
equal to employee regular price.
-
5:06
Next, one more else if to say Is it an employee and is it a matinee?
-
5:15
If it is, we'll assign the customerPrice of
-
5:18
employeeMatineePrice which we know is a free movie.
-
5:23
And finally, we'll say that any customer who doesn't meet
-
5:26
one of the above conditions, must pay regular price.
-
5:29
For that, we're gonna use
-
5:34
else customerPrice = regularPrice.
-
5:42
We're saying if none of these conditions, this if, this else if, else if, else if,
-
5:48
if none of those are met, and here we go, the customerPrice is the regularPrice.
-
5:54
Okay, in a second, we can assign some test values and run the code.
-
5:58
But before we do, let's just walk through everything that we wrote so far.
-
6:02
This is a really good habit to get into.
-
6:05
So up here, first we created three bools.
-
6:07
They were gonna hold whether or not the customer has an age discount,
-
6:12
whether the showing is a matinee, and whether the customer is an employee.
-
6:16
Next, down here, we created two variables,
-
6:21
one to hold the customerAge and one to hold the customerPrice.
-
6:27
After that, we created five floats to hold our different prices.
-
6:31
RegularPrice is the highest, ageOrMatinee, 8.50, ageAndMatinee,
-
6:37
6.50, employeeRegPrice, 4.50, employeeMatineePrice of $0.00.
-
6:43
Next, we needed to assess if the customer would get the age discount.
-
6:48
To do that, we checked if the customerAge was less than the youthAge,
-
6:53
which we set as 13.
-
6:54
Or is the customerAge greater than or equal to the seniorAge of 65?
-
7:00
If either of those conditions are met, they get the age discount.
-
7:08
Below that, we created a series of conditional statements.
-
7:12
First, we said, if they have an age discount and it's a matinee and
-
7:17
they're not an employee, give them the customerPrice of ageAndMatineePrice,
-
7:23
so that's the 6.50 price here.
-
7:28
Else if, as in this condition is not satisfied, let's evaluate
-
7:33
if they have the age discount or the matinee and if they're not an employee.
-
7:39
If that whole condition is true,
-
7:42
then we get get the customerPrice equal to the ageOrMatineePrice of 8.50.
-
7:49
Else if, as in if neither of these conditions were met,
-
7:53
let's check if they're an employee and if it is not a matinee,
-
7:57
if that's the case, employeeRegPrice of 4.50.
-
8:02
Else if, so if none of these conditions are met,
-
8:06
we'll check are they an employee, is it a matinee?
-
8:10
If yes, then the customerPrice is gonna equal the employeeMatineePrice,
-
8:14
which is free.
-
8:16
Lastly, we have an else statement to say if none of these conditions are met,
-
8:22
assign regularPrice of $10 to the customerPrice.
-
8:27
Okay, that all seems to make sense, so let's set some dummy values and
-
8:31
run our code.
-
8:32
First, let's set our bool isMatinee to true and
-
8:37
we'll set our bool isEmployee to false.
-
8:41
We can also set the customerAge to 70.
-
8:46
Okay, we're gonna throw a break point down here and we'll run our code.
-
8:56
If we mouse over customer price, we'll see that it's 6.50.
-
9:02
Just what we expect for someone who qualifies for
-
9:07
the age discount and is seeing a matinee.
-
9:12
Next, let's try the bool isMatinee equals false.
-
9:19
And we'll leave their age the same.
-
9:23
Actually just for kicks, let's make the employee true.
-
9:27
So they're not seeing a matinee but they are an employee.
-
9:30
We'll run it again.
-
9:36
Here we see our customerPrice if we mouse over is 4.50.
-
9:41
That's just what we'd expect for
-
9:43
an employee seeing a showing that isn't a matinee.
-
9:48
All right, let's try one more.
-
9:49
Let's make the isMatinee false, we'll make isEmployee false,
-
9:54
and let's change the age to 20, and we'll run it again.
-
10:01
Here we see our customerPrice is $10.
-
10:05
That's just what we expect for someone who wouldn't qualify for
-
10:10
either of our age discounts, seeing for
-
10:13
showing that is not a matinee, who isn't an employee.
-
10:17
That would be the regularPrice of $10.
-
10:22
Now of course in the real world, you would want to try many more tests with different
-
10:25
combinations, but it looks like we've got this working.
-
10:28
Before we go, let's be sure to remove these plug values up here
-
10:35
so they're not lingering around the next time we open the project.
-
10:38
Having those in there can lead to a lot of confusion.
-
10:42
So it's best just to clear them out.
-
10:44
I hope this example has given you some idea of how helpful and, or, not can be.
-
10:49
I also hope that while you may have initially cringed when you saw two
-
10:53
ampersands right next to each other, or an exclamation mark at the start of a word.
-
10:57
Now you know that those are just convenient ways of writing logical
-
11:00
operations you deal with every day.
-
11:03
No need to be scared at all.
-
11:05
Please take some time to work through the quiz questions, and
-
11:08
don't forget to floss regularly.
You need to sign up for Treehouse in order to download course files.
Sign up