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
Learn how to use Else if to create larger more complex conditional statements.
-
0:00
In our last video we learned how to use an if statement
-
0:03
to create a two pronged fork in the road.
-
0:05
In our case we wanted to decide whether or
-
0:08
not to give a senior discount to our theater customers based on their age.
-
0:12
However, we just found out that we also need to give a discount for
-
0:15
all the customers attending the first showing of each day,
-
0:18
which is often called the matinee.
-
0:20
Specifically, all customers regardless of age should only pay $4 for the matinee.
-
0:27
Okay, here we are back inside our box office project.
-
0:30
Let's incorporate that new matinee discount.
-
0:33
First of all, let's create a bool so we know if it's a matinee or not.
-
0:38
Next, let's add a matinee price.
-
0:47
We can add it to our business rules up here, too.
-
0:56
Also, I see that our variable here, discount price.
-
1:00
Now it's too vague.
-
1:02
Which discount are we talking about?
-
1:03
Let's change that variable name to something more precise and descriptive,
-
1:07
like senior price.
-
1:11
Hm, oh.
-
1:14
I see red.
-
1:15
Quick!
-
1:16
Quick!
-
1:16
Why do we get red?
-
1:17
That's right.
-
1:18
It's because we changed our variable name up here, so X code has no idea what to
-
1:24
do with the discounted price, because we never declared it.
-
1:27
Now I could simply type in, senior price, over here to get rid of the warning, and
-
1:31
that would work, but just for fun, let me show you another way.
-
1:34
Let's change our discount price variable name back to disc price.
-
1:39
Then we right-click, select Refactor, rename.
-
1:44
Then we enter our new name for the variable, which is senior price.
-
1:52
Now this wonderful window shows up,
-
1:54
showing everywhere in our project where the variable might show up.
-
1:59
Now for our tiny example this isn't all that impressive.
-
2:02
But in a real app a variable could show up dozens or
-
2:04
hundreds of times possibly in dozens of different files.
-
2:07
In that case, this is an extremely useful tool.
-
2:10
By the way, if you don't know already, refactoring code is when you improve
-
2:15
the internal workings of your code without changing the external behavior.
-
2:19
It's something programmers do all the time, and
-
2:21
is essential to writing good code.
-
2:23
Much like writing a song or a novel, folks don't usually get it
-
2:26
just right the first time around, and it gets better with editing.
-
2:29
Okay.
-
2:30
So now that we have this third price, it seems like, instead of a simple
-
2:34
two-pronged fork in the road, we would like a way to offer three choices.
-
2:38
The way we do this is with an else if.
-
2:41
An else if may sound a lot like an if, but it's actually quite different.
-
2:45
To illustrate the difference,
-
2:47
let's first try to get the job done using just a series of ifs.
-
2:51
For that,
-
2:52
we might try adding another if statement after we ask about the customer's age.
-
2:57
Specifically, we would add an if with the condition
-
3:02
if is matinee then assign the matinee price.
-
3:11
Now at first pass this might seem like it would work and it would for
-
3:15
some cases but what happens when a 70 year old shows up for an evening movie?
-
3:21
The way our code works they would satisfy this condition and
-
3:25
their price would be set to senior price.
-
3:28
Then we would test the next condition.
-
3:31
Is it a matinee?
-
3:32
No. And that would cause us to skip
-
3:35
this block of code here, and hop in here.
-
3:39
And that would not be good.
-
3:41
Because then it would charge them the regular price of $10.
-
3:44
The fix for that issue is using else if.
-
3:48
Let's delete all of our current logic and recreate it from scratch.
-
3:51
It'll just take a minute.
-
3:54
So first let's check if the matinee, since that's the lowest price we offer.
-
3:58
If this matinee equals yes, we'll set the customer price equal to the matinee price.
-
4:03
If isMatinee custPrice = matPrice.
-
4:13
Next we'll want to say only if the last condition was not met
-
4:18
here's another condition to test.
-
4:20
Specifically is the customer age greater than or equal to the discount age?
-
4:25
So we'll say else if
-
4:49
Lastly we can say that if neither of the conditions above were met then
-
4:53
assign the regular price to the customer price.
-
4:57
You see using that else if effectively changed what the last else
-
5:01
down here really does to our flow.
-
5:04
What it does now is only catch cases where none of the prior conditions were met.
-
5:10
Had we just used an if, it would catch any cases where the customer was under 60,
-
5:15
even if it had satisfied the matinee condition.
-
5:19
To demonstrate how this is actually working,
-
5:21
rather than simply what answer we get at the end,
-
5:23
I'll add a new break point up here, and we'll add one down at the end.
-
5:29
And we'll see how it executes.
-
5:31
I'm also gonna set our customer age to 75 and our isMatinee to Yes.
-
5:41
Okay, let's run it.
-
5:43
All right, we've stopped right here at the first break point.
-
5:47
Now, I wanna step, line by line, through the code we're executing.
-
5:50
Now, just to be clear, we won't be stepping through all the code we wrote,
-
5:53
just what we're actually executing.
-
5:56
To do that, we use this arrow right here.
-
5:59
By the way, this is most certainly not a full lesson in debugging, but
-
6:03
I wanted to use this tool to illustrate our flow.
-
6:05
When I click this arrow we'll evaluate the isMatinee condition.
-
6:09
Okay you'll see that after checking that condition
-
6:12
we wound up inside of our first curly braces where we assign the matinee price.
-
6:16
Great that's just what we wanted.
-
6:19
Now let's see what happens when we click the down arrow again.
-
6:24
Well one click just gets us to the curly brace and
-
6:27
then the next click well that took us all the way to our last break point.
-
6:31
We skipped everything else.
-
6:33
If we peek right here,
-
6:36
we'll see that our customer price is equal to $4, the matinee price.
-
6:40
We hit our initial clause where we assigned the matinee price, and
-
6:43
then we skipped the rest.
-
6:45
Let's try making it a non-matinee and then we'll see what happens.
-
6:55
We run again, and of course we stop at our first break point.
-
7:01
Let's step through.
-
7:04
Huh?
-
7:05
We skipped our first section since the first condition was not met.
-
7:10
Now, when I tap the arrow again, we'll evaluate that condition.
-
7:14
The customer age, which was 75, is greater than the minimum age, so
-
7:19
we wind up inside this curly brace where we're gonna assign the customer
-
7:22
the senior price to the customer price.
-
7:26
Click again.
-
7:28
We get to the closing curly brace, and then once more, we go
-
7:31
all the way to our final break point, where we skip this final else clause.
-
7:36
We mouse over the customer price and
-
7:38
we see we've got the correct senior price of $5.
-
7:41
Let's try one more case, setting the age to something lower, like 15.
-
7:48
We'll run again.
-
7:52
Okay, we've stopped at our first break point.
-
7:55
Step through, we're gonna evaluate is the customer age greater than or
-
7:58
equal to the minimum age?
-
8:00
It's not.
-
8:01
So we wind up here in our else clause
-
8:03
where we're gonna assign the regular price of $10.
-
8:06
For those keeping track at home, we've just tried
-
8:09
three of the four possible combinations of age ranges and isMatinee.
-
8:14
We didn't yet try a person under the age of 60 who is attending a matinee.
-
8:18
Now I won't spend time doing that now but
-
8:21
you'd certainly wanna do that in the real world.
-
8:23
Also, this is a good example of why we usually implement something
-
8:27
called a unit test.
-
8:29
Unit testing is a way to create persistent tests for the code we write so
-
8:34
we know that it can properly handle all the possible values you might throw at it.
-
8:38
For example, what happens if you,
-
8:40
the developer, change some of the inner workings of your code?
-
8:43
Unit testing is the accepted way to formally create, perform, and
-
8:48
reuse those types of tests for individual parts of your code.
-
8:51
They make sure that they function as you think they do and stay that way.
-
8:56
Keep plugging along with iOS, and before too long,
-
8:58
you'll find yourself needing XCTest, which is Xcode's unit testing feature.
-
9:03
But don't worry we definitely aren't there yet.
-
9:06
If you're just dying to learn more about unit testing
-
9:08
I've included some links below about xci test and about unit testing in general.
You need to sign up for Treehouse in order to download course files.
Sign up