In this listen, I am going to introduce the switch structure to you.
OK, so we are going simply to modify this.
We are not going to change anything in the program.
So please make sure that you know how to make the numbers OK.
I'm simply going to modify this structure with the switch.
So we have not seen the switch before because actually you don't need a switch that often and it only
works in specific situations like here when you basically need to check, OK.
You have an integer, OK, or you have a wrong number can be an additional number as well.
And you simply want to take different values for the number.
OK, so you can use that if anything else, else, cetera, etc. that's going to work and that is perfectly
fine.
But you have to know there is another structure that is maybe a little bit more reliable.
Some people prefer to switch.
Some people prefer that if if I'm going to show you the switch and then you can decide for yourself.
So let's commence this.
I'm going to do commence.
So the first thing you need to have is first what you want to test.
So here we have an integer command, which is what we get here.
So if you have the version two, you are going to do we have the original structure.
You are going to do results that value certain results that value for versioned and for version three.
That's.
And now the switch will be the same for version two and three.
So let's just right the switch so you can understand.
So switch it.
You can see it turn green.
You're going to switch and in parentheses.
And that is what you want to test here, the command.
And then you are going to open and close curly brackets.
And in that switch, you are going to use the key keyword case.
And for the case, for example, you are going to first try your case where the command is equal to
I am button zero.
So you two keys and then space and then what you want to divide you, you want to test and then a column
and you go back to new line with indentation and you can do some action, let's say celluloid print.
Ellen Button zero.
OK.
And then that's very important, you need to use the word break and then you go back to a new line,
you can do Kaze.
I button one with a column and go back to New Line so that.
Rent Ellen Button one.
And then, Rich, don't forget the break, so you can see you can do as many case as you want.
Case for button zero, one, two, three, nine, whatever, this is simply the same as if you use if
if you forgive it here, maybe you have a better way to.
It's maybe more ready.
OK, one thing here you have keys and then you don't have any curly braces here and well that's going
to work.
But I'm actually not a big fan of this, so I'm going to add braces here and here.
So you may add or not add those curly braces, but I'm going to add them into programs.
So the value column and braces here and then inside, you know that this is the block for this.
So you do whatever you want.
So this is just one line, but you can use as many lines as you want, called functions, etc. and then
you use the break that you use that for all the cases you want to test and then at the end.
So actually, these are the keys here to make the exact same.
He Sanyal dot print, Ellen button play.
And don't forget the bridge and then you will have so if you want to, else you can use default.
So basically this would be the switch when you switch with command.
This will first check if the command is equal to that.
If, yes, you execute the look of code and because of break, then you break out of the switch.
That's important because if you don't use break, it's going to continue with the different blocks of
code.
So first we check that if yes, we go here, we break out of the switch.
If it's not this, we check this one with addiction and we break.
If it's not, is this we check this one, OK, etc..
And if we have checked all the three and nothing corresponds to the command, then we are going to go
into default.
Bluck and let's say a print and then let's say invalid data.
So that is the sense that we have here and know that this is important in the default, you don't have
a break.
I mean, you could use a break that will not change anything, but you don't have to write one.
And it's very important, actually, that you create a default case for your speech.
This is not mandatory means that if you don't write this, your program will still work.
But this is a super, super important best practice that you use a default when you use a switch.
So if all of the cases are checked, but none of them correspond to the number you try to check here,
then you are going to get the default.
So you can, for example, print the message, set an error, flag our assumptions.
All right.
So to recap what you want to try here, what you test should always be around numbers that can be an
integer that you use, for example, with decimal number or hexadecimal number.
And the same can be a binary data.
It can be a long number.
OK, but don't use float.
Don't use string.
OK, only round number.
OK, and then you use a case statement for each case you want to test.
You use break at the end of your keys every time and then at the end you have a default.
If the command does not correspond to any of the keys you have added you.
OK, so you can see here the switch and if it's OK, that's the exact same thing.
But the switch is quite appropriate for that.
Use keys, OK, when you just want to test the value.
So that's why I'm only showing it to you now, because that's basically the only use case when you need
a switch.
So now let's run the program to see if it works.
OK, let's open to stand tall and let's price one, for example, button one next price to invalidate
all its price, zero.
We have button zero and it's price would we have played button plate and any other?
Stuff that the press we give invalid data.
OK, so that was the exact same way it worked with if Essive and as you can see now, if you have correctly
defined he and not your button, whether with version two or three and using the decimal hexadecimal
base for the number.
Well, the code is the same.
OK, all right.
So now you know how to use the infrared remote control.
You know how to map the different buttons in your code and you know how to handle each case to do an
action for each number you want.