WEBVTT

00:00.390 --> 00:05.940
In this lecture I want to go over a different control flow structure that's able to make decisions.

00:06.080 --> 00:08.770
The switch statement.

00:08.930 --> 00:12.740
So here's how we make a switch statement.

00:12.730 --> 00:22.500
You start with surprise surprise the word switch is a key word in C++ and we have in here the parentheses

00:24.000 --> 00:33.470
urgency expression for now and this expression is what gets evaluated so much like the if statement.

00:33.470 --> 00:38.350
This is kind of the condition but it's a bit slightly different.

00:38.360 --> 00:40.270
You'll see why.

00:40.370 --> 00:45.990
So the next thing we have is something called a case.

00:46.040 --> 00:49.850
So these are all the different cases that can happen.

00:50.380 --> 00:57.980
And the cases are based on some constant expression.

00:58.370 --> 01:08.640
And again you'll see an example preceding and between this after the semi or this colon here we'll have

01:08.640 --> 01:13.800
all our statements like before statements go here.

01:13.840 --> 01:14.040
DE

01:17.550 --> 01:26.460
and optionally have something called a poor make statement as is optional and I'm not going to go over

01:26.460 --> 01:27.190
this quite yet.

01:27.200 --> 01:28.060
But ules.

01:28.080 --> 01:31.650
You'll see in a second what this is for.

01:31.710 --> 01:41.820
So you can have more Case statements Onsen expression say two and you can have as many as you want.

01:45.100 --> 01:46.600
OK statements

01:51.650 --> 01:57.880
in this last thing is kind of like the L's case in our IF statement last time.

01:58.280 --> 02:00.560
But we're going to call it the default.

02:00.560 --> 02:04.870
And this is the catch all for this switch statement.

02:05.330 --> 02:15.000
So if if somehow the expression didn't match one of these cases it would go to the default and do and

02:15.000 --> 02:16.550
do this.

02:16.860 --> 02:27.240
This set of code default statements.

02:27.290 --> 02:30.240
So again there's lots to unpack here.

02:30.470 --> 02:33.950
So the expression here Mohs.

02:34.190 --> 02:38.940
So this must evaluate to an integral type.

02:39.080 --> 02:45.320
So an integral type could be something like an int or could be a char.

02:45.320 --> 02:55.190
This is also an integral type or it could be a boolean that's also considered a integral type.

02:55.660 --> 03:07.380
So it has to evaluate to some something like an int or a char or a can for our purposes and here let's

03:07.380 --> 03:20.190
say let's say we use just X and we said indexical 0 K and the constant expressions here.

03:20.210 --> 03:22.880
This is just some constant value.

03:22.880 --> 03:29.780
So say let's say in constant 0 here.

03:30.410 --> 03:32.690
This is a constant expression as well.

03:32.690 --> 03:36.380
0 is just constant expression.

03:36.380 --> 03:41.350
Or here we can have one let's say.

03:41.720 --> 03:43.820
And that's also a constant expression.

03:45.310 --> 03:48.970
So let's just see a little example of this.

03:49.000 --> 03:51.260
Let's see Ouch.

03:51.310 --> 03:52.650
Case 0

03:57.110 --> 03:59.250
in C H.

04:01.910 --> 04:03.910
Case 1.

04:07.040 --> 04:14.200
And here in the default we will see a c h you fault's case

04:18.740 --> 04:22.280
this and that's right.

04:22.340 --> 04:26.810
You don't need these right now.

04:27.140 --> 04:28.160
And through this

04:36.500 --> 04:39.090
their case zero.

04:39.560 --> 04:43.500
So we assign X the value.

04:44.030 --> 04:51.230
And it's a switch on X and then it'll match this constant expression.

04:52.210 --> 04:59.290
Whatever it is say X is 1 Greenwich it did Case 1.

04:59.330 --> 05:00.760
Is this right here.

05:00.770 --> 05:09.450
So one is matched to this case 1 and then it did these these statements.

05:09.610 --> 05:20.300
Let's say do many geese one is d and s.

05:21.450 --> 05:23.130
To say

05:28.430 --> 05:38.710
right in this case one with the exclamation and just ones to gain so many statements.

05:38.710 --> 05:44.240
So what this break here though this is kind of weird right.

05:44.530 --> 05:50.400
Well this break will effectively just break out of the switch statement.

05:50.590 --> 05:54.220
Once it's once you get to this point.

05:54.250 --> 05:56.050
So it did give us one.

05:56.050 --> 05:59.460
And then it said case two or so he said.

05:59.470 --> 06:08.490
Case one in in case one statement two and then it hit this break point or is not breakpoint but break

06:08.490 --> 06:09.220
state.

06:09.470 --> 06:10.460
Right.

06:10.770 --> 06:19.480
And then it broke out of the switch entirely and came here to line let's say 39.

06:19.510 --> 06:41.490
So let's just say out of switch statement.

06:41.930 --> 06:45.950
So case one case one statement two.

06:46.190 --> 06:53.390
And then out of switch statement so it skipped say this default case.

06:53.810 --> 06:56.710
So let's see what happens if we didn't have this break.

07:01.140 --> 07:08.510
So it says case one case one statement two and then defaults case and then you're out of the switch

07:08.540 --> 07:09.330
statement.

07:09.780 --> 07:14.950
So like I said before it all go.

07:15.080 --> 07:18.510
So it'll switch on x.

07:18.540 --> 07:26.960
It'll find the first case that matches X and then it'll do every case below this one.

07:26.970 --> 07:39.450
So will do every case below case 1 until the end or until it hits a break and then it'll exit the switch.

07:39.450 --> 07:49.350
So this is a little bit different than if and else if control structure that we did last time.

07:49.960 --> 07:51.580
So that's why we need the break.

07:51.580 --> 08:01.120
So you'll usually see break for each case because usually we don't want to do cases below the case that

08:01.120 --> 08:02.260
we matched.

08:02.350 --> 08:06.920
Usually sometimes you do but usually do we don't.

08:06.970 --> 08:13.180
So one interesting thing about switch statements is you can't do something like this.

08:13.300 --> 08:22.530
Y equals zero.

08:22.750 --> 08:24.030
Run this.

08:24.190 --> 08:27.360
So air exists it's pointing to this.

08:27.400 --> 08:29.380
Why doesn't.

08:29.530 --> 08:31.100
It doesn't like this for some reason.

08:31.390 --> 08:43.150
So if you want to make new variables inside of a case you have to do this you have to use the braces

08:43.150 --> 08:44.930
that you talked about earlier.

08:46.310 --> 08:52.050
So just brace around the case say it why 0

08:55.480 --> 08:58.020
then this should work.

09:03.570 --> 09:04.230
Like that.

09:04.550 --> 09:06.500
So it doesn't mind it now.

09:06.890 --> 09:14.450
So just remember if you ever want to make new variables inside of a case for a switch state and always

09:14.450 --> 09:21.450
just wrap it around these curly braces.

09:21.500 --> 09:29.630
In fact what I usually do for switch statements in and generals I always make curly braces for the case

09:29.720 --> 09:31.520
every time.

09:33.160 --> 09:37.820
And that is I just wrap until the break and then that's it.

09:37.890 --> 09:47.520
This and that's usually how I write my switch statements.

09:47.680 --> 09:53.140
So let's see a real example of this.

09:53.250 --> 10:08.710
So it's delete lead on this.

10:08.770 --> 10:19.930
So let's solve the please enter day of the week from 1 to seven problem that you solved in the first

10:19.930 --> 10:20.420
section.

10:20.440 --> 10:35.300
So let's have a day equals zero saying ouch E's teacher day.

10:38.540 --> 10:39.560
6:59

10:45.850 --> 10:55.820
see in a and then three switch statement switch on the day.

10:56.050 --> 11:03.010
Case on remember we start the day on Monday right.

11:03.060 --> 11:05.410
So one is Monday.

11:05.510 --> 11:06.580
See out

11:10.150 --> 11:10.830
Monday.

11:11.810 --> 11:19.880
I'm not going to rate the braces for this like that and break it.

11:19.940 --> 11:21.950
We don't want it to fall through.

11:21.950 --> 11:35.530
If it was Monday or you don't want to do the cases below case 1 if it was Monday case to see Ouch Tuesday

11:38.260 --> 11:43.090
and all of the break.

11:43.380 --> 11:44.400
Case 3

11:47.570 --> 11:48.010
CH

11:51.020 --> 11:51.660
Wednesday

11:56.500 --> 11:56.630
the

12:01.210 --> 12:02.710
case for

12:06.000 --> 12:06.840
Thursday

12:20.440 --> 12:21.280
5

12:26.800 --> 12:27.490
Friday

12:35.290 --> 12:36.340
six

12:42.070 --> 12:42.850
Saturday

12:53.490 --> 12:56.660
seven C..

12:56.750 --> 12:59.370
CH Sunday

13:02.660 --> 13:06.740
and remember we had the default case B

13:09.770 --> 13:21.210
say entered something outside of 6:59 say that was not such a valid day.

13:28.800 --> 13:30.420
Spell it wrong.

13:32.330 --> 13:45.310
Ballasted and through this so please enter day of the week so say 6 so it's Saturday.

13:45.540 --> 13:47.560
You should read this again.

13:47.670 --> 13:49.710
So we said negative one.

13:49.960 --> 13:54.360
That wasn't about day so it went to the default case.

13:54.530 --> 14:04.670
In this case the negative one is so see if we did three or remove all the breaks

14:08.210 --> 14:11.370
all the breaks from the tires which state and see what happens.

14:17.710 --> 14:20.140
So let's say it's Tuesday.

14:20.440 --> 14:25.900
So the day of the week is Tuesday Wednesday Thursday Friday Saturday Sunday and that was not a valid

14:25.900 --> 14:26.520
day.

14:26.950 --> 14:32.130
So see how it might fail through.

14:32.270 --> 14:36.770
So it matched to two notice it didn't match to one at all.

14:36.770 --> 14:39.390
There's no Monday being out put it here.

14:39.620 --> 14:44.140
So it it to and then it did every case below 2.

14:44.630 --> 14:48.950
So that is why you need the break statements

14:52.890 --> 14:57.890
so always remember that.

14:57.940 --> 15:03.410
So in the next lecture I'll show you even one more way of making decisions.
