Welcome!
Now let's talk about the switch statement. A "switch" statement is similar to an if statement but with a
different syntax. there are two types of switch statements actually, expression switches and type switches
but for now you're only gonna learn about the expression "switch" statement you'll learn about
the type switch later because it's related with interface types.
OK.
All right let's get started.
Now I'm going to start easy and build up your knowledge step by step.
As usual this is at that you want to write a program that will print the name of a country for a given
name.
For example if the is in Paris then it will print France or how French people say it like finds it.
I don't know whether I spell it correctly or not and Twice.
OK I have a variable called the and it equals to parse.
All right here is a sorry statement.
As expected it starts with us.
Each q r.
Ok as I said I want to find paresis and I have a city where able.
So I need to type to the variable here like this is you'll see in a minute pseudo statement view.
Look up for it is value defined Purus.
I need a case clause inside the switch statement like this.
Since I'm looking for purpose I'm going to type Paris with a column after what's OK.
Now let's take a look at the parts of this statement in the
highlighted part is called Subi just condition expression.
The type of this condition expression is very important because it determines the type of the case conditions
Steve were able.
Here is a Swink value right.
So the type of all the case conditions should also be strange.
That's why Parvis there is a string value.
It cannot be an end value for example but if the switch is condition which is still able.
Here was an int and the type of the case conditions would also be an invalid OK.
Now let's take a look at the case clause in detail.
This highlighted part is a case Klause or simply a case after the column here go creates a block Xclusive
only forty's case class.
So anything that VB declared inside this case class you only ever label to itself.
OK and this highlighted part is a case condition expression or simply a case condition.
So run the steamer variable equals to Paris go execute these case clauses block and here in this block.
There is only Vonne printed on call right.
So go execute it and it will print France to the council.
It's like typing in an IF statement like this so as so each statement compares the given value to all
of its case Klaus's in terms of equality.
Here it will execute this case clause because you are able and the case clauses condition equals two
parts is by the way.
Of course you can also add more cases to us which statement.
Now as an example let's add another step.
For example Tokyo although it's distric but let's call it here as a city OK.
So all I need to do is to add another case clause like this.
Dumb going to print its country as Japan
does so his statement just compares the stairway.
For each case condition one by one and if one of them is equal to the serious condition dango Rantz
the code.
In that case just block OK let's say that this is tōkyō down to talk.
Your case will match then this block will be executed.
Remember it's block starts after the column here so it will call the preinstall and there and it will
print Japan to the console.
If this the last part is done the first case would be executed instead and it will print.
France OK let me show you this whole switch statement inside decoders are in action so you can understand
it better.
Now let's change the city to Tokyo.
Here that's it.
This time it pre-NST Japan.
This is because now here equals to this case condition here right.
So it's wrong.
These instead remember a silly statement just compares the values whether they are equal or not.
And that's all about it.
Now let's take the rally from the common line as an argument so let's change talk here to Oist not ours.
Instead like this what in Litmus right again it brings France correct.
OK let's try to talk you know it brings Japan good.
It works Litton type this as an IF statement.
I'm going to fast forward this.
So you don't need to wait for me to type it here.
OK.
All right.
Here it is.
So the statement actually equals to this statement here.
For example in C Savvich statements cannot use runtime expressions but Ingall you can use an expression
whether it belongs to runtime or to compile time.
Doesn't matter.
This is because behind the scenes go Comverse a civic statement into an IF statement.
So you can use any type of expression as conditions ok for example it and rented Parus both of the statements
in France because there are similar statements.
So you can think of that.
Each case corresponds to an Eve an hour.
If branches in an IF statement.
OK OK now let's talk a little bit about the rules of the switch statement.
The first rule is that values inside the case conditions should be unique among the other case conditions.
So there can be only one case condition with the same value.
Right for example that change Tokyo to Paris like this.
As you can see gold warns me that there are duplicate case conditions.
All right let's take it back.
The second rule is that the typesof to switch and the case condition expressions should be comparable.
For example that may change these part to a number.
For example going on says that the steamer table and this number doesn't match because the stairway
is a string.
However the case condition here is an int so each case condition should always match to this to each
condition expirations type.
It may take it back by the way for coming from other languages to go down.
You may be wondering how this works.
Without the break statements there is a break statement Ingo as well.
However it is not necessary because it adds down for you automatically.
So if we go first don't use it.
Most of the time.
Let me add a few break statements to this search.
As you can see it still works but it's not necessary to add them.
I'll show you later that they can be useful.
And lastly let's talk about the exclusivity of case Klaus's.
As I said each case Clouse creates its own exclusive block right.
So for example declare a variable here V.I.P true and this printed printed and VIP trip.
Ip.
OK this V.I.P variable is only a label inside these case block.
For example this duplicate this line here and move with your as you can see there is an error.
Now go says that VIP table doesn't exist here.
Or let's move with your as it can see.
These kids can also see the we will write.
So in summary case block creates its own exclusive block.
All right.
Thank you for watching.
That's all for now seen the next lecture.
Baba.