Kind of again in this list then we're going to talk about switch switches essentially another control
flow command either helps us with organizing our nested if else statements.
Let's see how we do that.
The school had to exclude in school I'm going to close this.
Start your project command line tool call it sewage project and then my switch project.
The first thing I'm going to do I'm going to show you what is the problem you're trying to are to answer
essentially using a switch statement.
Imagine a situation like this.
Imagine if you had an integer card my ends which is currently a value such as for.
Of course as you know I'm putting a static ideas in here with in real life application.
This idea would be fed to us from another resource.
Now imagine I want to find the what is this likely one based on that do something.
So if it is what I want to do something for this one.
One other thing if this so on and so forth it would become something like this if my Int is zero then
you do something else.
If my end is one then obviously you do something else and then else if my end is I'm not to then the
compiler had to turn something else.
And as you can imagine this list of if else if else if I was keeping freezing depending on my situation
why there's nothing wrong about coding like this there is a much better structure and that's called
a switch.
The switch works based on different cases and so you provide an expression that is your value and then
you provided some cases and based on each case you decide about what you want to do.
So let's come this into a switch statement.
Let me first write a switch on the auto completion gives us some values in here.
It shows us that for each unit to provide me an expression and then in the body of the switch you have
to give me the case statements you want to run on break.
So let's see how that actually works.
I'm going to say the statement the expression that I want to switch is my in the case for it is if it
was a 1 then and this log in it was one for instance then I'm going to say that's one case and then
I have another case.
And then in that case if case was to then say two and then break and then I'm going to say another case
if it was three then right three and me read in then everything nice and tidy.
So as you can see the writing of it is not necessarily shorter than writing it.
If a state has a statement or an if else statement but it's a little bit more readable because you can
see that in case it was one do this thing in case it was to do with the other things so on and so forth.
And of course you need to have a default value that happens if none of them was actually valid actually
going to add that the number say in the default say log on none of the above.
None of them was actually correct.
So that's essentially the structure of a switch is not very difficult.
And you know once you get used to it you can use it for situations when your if else statement has multiple
different situations.
So you will need if else if and for that I would suggest that you use switch and other thing that I
want to mention that is in it you can actually combine some of your cases.
So for instance if you need a case of one and two to be considered the same you can actually do this
you can say case one case two it is or it was one or two.
So right now most of these cases they refer to this part of the court.
And then of course there's a break after it which means you get out of the switch because of this system
where the case gets compiled and calculate that.
And that break happens after it switches off often more efficient than using an if else statement.
If you have reach millions of entries or if else with millions of entries then switch would work a little
bit faster in saying that that's really left up to your judgment depending on what you're doing and
whether you feel like you know if is a better choice for there.
It's an easier quote to right.
Or whether a switch would be a better option.
Now one viewer here I should mention something that switches often constrained that we use to be used
with numerical values.
For instance if an if statement you could do something like this in if else you could say if I don't
know all my name is equal to a string by name.
Exactly.
Then to do something which means in the IF expression we are actually comparing two values of the type
in a string.
Unfortunately this switch doesn't provide much infrastructure.
You cannot do something like this you cannot say switch.
And then you need say a string and then for the cases how the strings again because switch doesn't accept
and as a string as a value.
So something you could do in here is this which is not really a very you know modern way of doing it
but they found out that sometimes they actually help me.
And here is how I would do it.
Imagine I have a string and that this thing has to be compare it against the names.
Vancouver San Francisco New York.
And I don't know maybe.
The other is Paris for instance.
So if I have to compare against this I put all of these within an array.
So I'm going to go ahead and say and this array or the strings array that is in this array and that's
all right.
I can't initialize with objects the objects aren't going to be in Vancouver and the other object is
going to be this code.
If that's how you type it I always have difficulty typing some something something I'm really good at.
And some others are in New York and then Paris.
OK.
So that's about that.
I need one more practice of course.
Give me a stack of them so we can see them nice and neat and proper So now we want to switch a certain
value.
Imagine my value as a string.
My strength is going to be let's say you are.
So we want to know if money is streambed is indoors.
We want to find out.
We want to use a switch for that if as statement here is something you could do.
You could say switch in the switch.
You need to provide an integer.
So I'm going to say integer my code is my integer let's call it city index.
That's going to be s t r array index of objects.
My A C R.
So I'm saying go to these things array and find what is the Index of this guy.
What's that index of New York in here.
So it's going to go 0 1 2 and he's going to find two.
So I'm going to say switch city index and then chase zero.
If it was under zero then I'd say this like it was first for instance or whatever that you wanted to
do for you another case would be case 1 for instance.
And yet I'm to say it was soccer and so on and so forth.
The only warning we have in here and I think we have seen this in the past that this is an integer.
This is a long unsigned.
So I'm going to say Forsett cast it into an integer.
So this would be another way for switching between a string ralliers of course is another very very
common practice.
But you know if you were wondering is it at all possible to switch string.
Yes you can pull all of your values inside an array and find the index of your object the one you're
searching for in there.
And as a result you can actually use it.
So it is kind of a trick because simply again switches using an integer but kind of gets the work done.
So I'd be happy with that.
So a quick recap of everything switches used in a sort of a nested series of if else statements switch
can switch different values expressions and for each of them it provides a case for each of the cases
you decide about what is going to happen and you'll break out of your switch and you can combine different
cases.
That's about that.
And I shall see you guys in the next lesson.