Hello and welcome.
So we're now going to discuss switches so a switch is like an if an if statement and then a false statement.
But it's a tidy way of doing it and you use switches when you have lots of different options.
So rather than having a massive if statement switches are much cleaner.
So let me show you how to create a switch.
First thing we need a value to compare against.
So let's go into the results equals zero.
And I want you to do switch result
and then what you can do is you can go.
Case 0 break.
Case 1 break case to break.
And now you can go print f k 0
printer Case 1 and so on and so on.
While this does it basically says if the result is zero run this code here if the result is 1 run this
code here if the result is to run this code here.
Now these these cases are not separate scopes.
What I mean by that is if you wanted to go into a here you couldn't do in a here and I want to make
that very clear.
If we can pull out now you can see there's a problem right now the way you get around that is you just
use curlies
because these curlies braces these curly braces can actually be used outside of old statements.
It could be like that and because they're two separate things you need to know this.
So here we say create a new scope where I can define variables inside you see that is legal if I do
it it should be and should be that is legal.
And if I didn't have these then it's not legal right.
Because then we define it to be we we declare it twice.
Well it's important to mention that because the same applies here.
These cases are not separate scopes.
So if you want to declare variables in them you you should use these curly braces.
Now let me explain a little more about what's going on here.
So cases are basically run if the result is equal to the case.
So if it's if the result zero case zero is run which will output case zero if Case 1 if result is is
equal to 1 in case one is run if result is case two then case two is run.
So it's just run that quickly.
And uh I'm just going to give it to this individual that I put here as well.
If we do Maine we can see as this case zero reset result to one you can see Case 1 if you set result
to 5
you'll notice you see nothing.
So how do we do an else in a in a in a switch.
Well you can use default printer if nothing's selected.
So what this does if I run that now nothing selected.
So if the result is not case zero it's not Case 1 it's not Case 2 then output nothing selected that's
what default is for default is basically an else if none of the cases can match go to default so there's
another clever thing you can do with cases here.
You can have multiple cases run the same code now to demonstrate this is important to understand break.
Now much like you know in our statements lecture if statements.
Sorry false statements.
And while loops you can break from a few remember break leaves the loop early while similarly you can
break from switches as well and all a break means is I'm done with this switch.
That's all it means.
So what happens is we come into case zero here we are put Casey rule we break and essentially leave
the switch now without breaking the what would happen is it would run Casey or one case one if you know
compile that and also we need to change our result to zero guys change your result zero.
You can see Casey your case 1 Now if this is one compile that again you only see Case 1.
So let me explain this.
One result is case zero which it is now.
What happens is the the actual code comes in here it jumps to here.
Case 0 get outputs Case 0.
And then because there is no break leaving this case it then runs on to case 1 and runs Case 1 and then
finally gets the break and leaves.
And that is why when we do cases where we see cases in case 1.
But if we do Case 1 we only see Case 1 because we don't see Case 2 because we break here on Case 1.
If you got rid of this break so that it s case zero case 1 case too if you now compile that you now
see case your case 1 case Code 2.
If you set result a 1 you see case one case 2 you set it to 2
and you only see case 2.
So that makes sense.
In other words the code just keeps running down until it sees a break.
And this is great because it allows you to kind of combine cases together which is pretty helpful.
So another thing we can do before this tutorial ends I can show you another thing.
Give it a case 1 and we're gonna call Case 0 and Case 1.
That's where we're gonna rename that to and put a brake here as well guys are now just on the case 0
2 Case 1.
So you can also do it like this.
So all this means is if it s case 0 then carry on running down on the case 1 and output this message.
And if it's case 1 then carry on running down and output this message.
So now if you put 0 0 1 as the results you can see Case 0 and Case 1.
If you for one is the result you can see case you in case 1.
So that is basically switches and they are amazing for comparing results like this.
So a final thing to know about switches your cases have to be constant values.
In other words I can't point this to a variable if I do this and I want to see if a is equal to 1.
I can't do case a that does not work.
You have to use constant values.
Now if you had a definition of the case value that would be fine because remember that's done at compile
time.
So if I did define Case 1 equals 1 and then in case 1 I changed it.
Case 1 that would work fine because remember the pre process of the compiler replaces this with one
during compile time because case 1 is equal to 1.