WEBVTT

00:00.150 --> 00:04.380
In this listen, I am going to introduce the switch structure to you.

00:04.680 --> 00:07.700
OK, so we are going simply to modify this.

00:08.010 --> 00:09.990
We are not going to change anything in the program.

00:10.200 --> 00:14.110
So please make sure that you know how to make the numbers OK.

00:14.370 --> 00:18.060
I'm simply going to modify this structure with the switch.

00:18.360 --> 00:24.270
So we have not seen the switch before because actually you don't need a switch that often and it only

00:24.270 --> 00:30.370
works in specific situations like here when you basically need to check, OK.

00:30.600 --> 00:35.970
You have an integer, OK, or you have a wrong number can be an additional number as well.

00:36.360 --> 00:40.320
And you simply want to take different values for the number.

00:40.360 --> 00:47.070
OK, so you can use that if anything else, else, cetera, etc. that's going to work and that is perfectly

00:47.070 --> 00:47.400
fine.

00:47.670 --> 00:51.570
But you have to know there is another structure that is maybe a little bit more reliable.

00:51.780 --> 00:53.350
Some people prefer to switch.

00:53.370 --> 00:58.820
Some people prefer that if if I'm going to show you the switch and then you can decide for yourself.

00:59.190 --> 01:01.230
So let's commence this.

01:02.070 --> 01:04.050
I'm going to do commence.

01:05.700 --> 01:09.240
So the first thing you need to have is first what you want to test.

01:09.270 --> 01:12.480
So here we have an integer command, which is what we get here.

01:12.750 --> 01:17.870
So if you have the version two, you are going to do we have the original structure.

01:17.880 --> 01:25.760
You are going to do results that value certain results that value for versioned and for version three.

01:25.770 --> 01:26.130
That's.

01:26.640 --> 01:30.570
And now the switch will be the same for version two and three.

01:31.470 --> 01:34.600
So let's just right the switch so you can understand.

01:34.620 --> 01:36.600
So switch it.

01:36.600 --> 01:38.280
You can see it turn green.

01:38.520 --> 01:40.830
You're going to switch and in parentheses.

01:40.830 --> 01:43.860
And that is what you want to test here, the command.

01:44.130 --> 01:47.190
And then you are going to open and close curly brackets.

01:47.730 --> 01:50.850
And in that switch, you are going to use the key keyword case.

01:51.570 --> 01:58.500
And for the case, for example, you are going to first try your case where the command is equal to

01:58.530 --> 02:00.180
I am button zero.

02:00.540 --> 02:07.620
So you two keys and then space and then what you want to divide you, you want to test and then a column

02:08.490 --> 02:14.520
and you go back to new line with indentation and you can do some action, let's say celluloid print.

02:14.520 --> 02:17.500
Ellen Button zero.

02:17.910 --> 02:18.270
OK.

02:20.070 --> 02:25.910
And then that's very important, you need to use the word break and then you go back to a new line,

02:25.980 --> 02:27.270
you can do Kaze.

02:29.050 --> 02:34.010
I button one with a column and go back to New Line so that.

02:35.380 --> 02:38.860
Rent Ellen Button one.

02:40.280 --> 02:47.780
And then, Rich, don't forget the break, so you can see you can do as many case as you want.

02:48.620 --> 02:54.710
Case for button zero, one, two, three, nine, whatever, this is simply the same as if you use if

02:54.780 --> 02:59.000
if you forgive it here, maybe you have a better way to.

02:59.030 --> 03:00.130
It's maybe more ready.

03:00.200 --> 03:08.030
OK, one thing here you have keys and then you don't have any curly braces here and well that's going

03:08.030 --> 03:08.540
to work.

03:08.960 --> 03:14.760
But I'm actually not a big fan of this, so I'm going to add braces here and here.

03:15.290 --> 03:20.960
So you may add or not add those curly braces, but I'm going to add them into programs.

03:21.770 --> 03:28.820
So the value column and braces here and then inside, you know that this is the block for this.

03:29.090 --> 03:30.570
So you do whatever you want.

03:30.590 --> 03:36.860
So this is just one line, but you can use as many lines as you want, called functions, etc. and then

03:36.860 --> 03:45.570
you use the break that you use that for all the cases you want to test and then at the end.

03:45.650 --> 03:50.420
So actually, these are the keys here to make the exact same.

03:52.800 --> 03:57.010
He Sanyal dot print, Ellen button play.

03:58.920 --> 04:07.010
And don't forget the bridge and then you will have so if you want to, else you can use default.

04:08.160 --> 04:13.130
So basically this would be the switch when you switch with command.

04:13.440 --> 04:16.410
This will first check if the command is equal to that.

04:16.410 --> 04:21.430
If, yes, you execute the look of code and because of break, then you break out of the switch.

04:21.450 --> 04:26.520
That's important because if you don't use break, it's going to continue with the different blocks of

04:26.520 --> 04:26.830
code.

04:27.210 --> 04:31.700
So first we check that if yes, we go here, we break out of the switch.

04:32.250 --> 04:36.120
If it's not this, we check this one with addiction and we break.

04:36.390 --> 04:40.170
If it's not, is this we check this one, OK, etc..

04:40.170 --> 04:46.170
And if we have checked all the three and nothing corresponds to the command, then we are going to go

04:46.170 --> 04:47.130
into default.

04:48.150 --> 04:56.310
Bluck and let's say a print and then let's say invalid data.

04:58.440 --> 05:04.050
So that is the sense that we have here and know that this is important in the default, you don't have

05:04.050 --> 05:04.470
a break.

05:04.470 --> 05:09.090
I mean, you could use a break that will not change anything, but you don't have to write one.

05:09.630 --> 05:14.790
And it's very important, actually, that you create a default case for your speech.

05:15.330 --> 05:20.430
This is not mandatory means that if you don't write this, your program will still work.

05:20.640 --> 05:26.700
But this is a super, super important best practice that you use a default when you use a switch.

05:27.630 --> 05:36.030
So if all of the cases are checked, but none of them correspond to the number you try to check here,

05:36.420 --> 05:38.480
then you are going to get the default.

05:38.580 --> 05:42.780
So you can, for example, print the message, set an error, flag our assumptions.

05:43.080 --> 05:43.410
All right.

05:43.440 --> 05:49.820
So to recap what you want to try here, what you test should always be around numbers that can be an

05:49.830 --> 05:54.690
integer that you use, for example, with decimal number or hexadecimal number.

05:54.690 --> 05:57.350
And the same can be a binary data.

05:57.360 --> 05:59.220
It can be a long number.

05:59.430 --> 06:01.590
OK, but don't use float.

06:01.590 --> 06:02.680
Don't use string.

06:02.680 --> 06:04.590
OK, only round number.

06:04.800 --> 06:08.970
OK, and then you use a case statement for each case you want to test.

06:09.240 --> 06:15.360
You use break at the end of your keys every time and then at the end you have a default.

06:15.720 --> 06:20.560
If the command does not correspond to any of the keys you have added you.

06:20.920 --> 06:26.700
OK, so you can see here the switch and if it's OK, that's the exact same thing.

06:27.300 --> 06:30.660
But the switch is quite appropriate for that.

06:30.660 --> 06:33.870
Use keys, OK, when you just want to test the value.

06:34.380 --> 06:41.010
So that's why I'm only showing it to you now, because that's basically the only use case when you need

06:41.010 --> 06:41.610
a switch.

06:42.000 --> 06:44.760
So now let's run the program to see if it works.

06:47.310 --> 06:55.590
OK, let's open to stand tall and let's price one, for example, button one next price to invalidate

06:55.590 --> 06:56.850
all its price, zero.

06:58.690 --> 07:04.390
We have button zero and it's price would we have played button plate and any other?

07:05.630 --> 07:08.150
Stuff that the press we give invalid data.

07:08.580 --> 07:17.060
OK, so that was the exact same way it worked with if Essive and as you can see now, if you have correctly

07:17.060 --> 07:25.520
defined he and not your button, whether with version two or three and using the decimal hexadecimal

07:25.520 --> 07:26.730
base for the number.

07:27.050 --> 07:28.490
Well, the code is the same.

07:28.540 --> 07:29.600
OK, all right.

07:29.610 --> 07:32.790
So now you know how to use the infrared remote control.

07:32.930 --> 07:40.070
You know how to map the different buttons in your code and you know how to handle each case to do an

07:40.070 --> 07:42.080
action for each number you want.
