And now in this video we're going to check out then as the expression.
Now this is actually the replacement of the switchgear statement that is found in C language or the
Java language.
So let us explore then as the expression as well.
So insert the intelligent ID here.
I have simply defined variable x equal to do and its value is final.
Now instead of writing the switch statement that we used to do in case of let's say Java here we have
the then statement.
So when X does index is same good as phase and then inside the code block let us write our called.
Lets see if X if the value of x is equal to one then simply put the dash followed by Arrow and then
simply write the call here.
But in line X is when.
Right and now.
Similarly if the value of x is equal to 2 then simply execute this line of code.
Let's see what inline X is do right now here let us when the called so we get the output as X is 2.
Now here.
Notice we don't have to write any statement ordered the code in the new statement in case of switch
case statement.
Unlike in Java or C++ where we use to date E-Street Band bench every We're now similar to Java.
We also have the forward statement in case of the end clause.
So the forward statement into oven is actually represented with the help of is right.
So we need to read as part here.
And again this index is bash followed by arrow.
Now are just print x value is unknown right.
And now here I will simply change it to legacy 5.
Let us note in the chord and in the output we have x value is unknown just because the x value is 5
and we have just read in the code for 1 and 2 not 4 5.
So the else part is executed and now we can also have the code block in case of inside the venue clause
such as the block here and then the block here.
Now inside which they can simply write our chords multiple lines of code such as print Elen x value
is unknown.
And also let us bring in I don't know what is x.
Let us run the code.
So here we get x value is unknown.
I don't know what is x no similar to as far.
You can also put the cord block here.
Like this.
And here again you can simply put the multiple lines of code inside this right like we have done in
case of ex-parte here.
And now suppose I have the value of let's say X equal to zero.
Now here if you want to execute the medical condition in just one statement then what you can do is
you can simply use 0 or more of an so here the x value is 0 or 1.
Right.
So you can combine multiple conditions here as well or you can also add three or four or something like
that.
But let's try it out with 0 and when.
So in the output console we have X is either 0 or 1 like this.
And now apart from this they can also use the range in case of values here let's say x is equal to 1.
Right.
And now suppose here if I use the range of when dot dot let's say 20 or let's change it to let's say
11.
And here I would simply use the in operator in space one dot dot Trente.
So this range of values contains a value from 1 to 20 which also includes live in as well.
So this condition will simply return true and this statement will be executed.
So here we change the print statement.
X lies in.
Let's see one two twenty eight so in the outward and so we have x lies in one 220 now also you can use
the not operator here.
Fine.
So the compiler and the condition becomes false here.
They simply look for the match here.
And again if we don't find a match then by default it will simply exit.
That is part here.
So now let us run the code.
So here we get x value is unknown.
I don't know what is X.
Right.
So you can simply use the range here.
In case of when statement and now their topic of discussion for this video was actually then an expression.
So suppose if I define a string Let's see that space SDR.
Let's see it is of that type of string.
And now if I want to print something here let's see if x is equal to 1.
And here let's simplify the code as well.
Print x is when.
Right.
So here what they really do is instead of printing some value I would simply assign SDR equal to legacy
X is 1 and then again it's are equal to let's see X is do.
And similarly in the as far as well I would simply assign SDR equal to x value is unknown and simply
remove the last statement right.
And at last I will simply print SDR.
And now let us the called.
So in the output we have X is when that is.
This statement has been executed and the value of SDR becomes X is 1.
Now in this way you can simply assign the values when some condition is actually matched.
Now similar to the statement.
You can also use the end statement as an expression.
So in that case what you will do is simply quoted from here.
Control X and then simply pasted just beside the main statement and then simply use that equally to
operator.
Now here what is happening is when this condition is actually matched then it will simply return this
value and this value will be assigned to this SDR variable.
Right.
So let us remove the SDR variable from here and that does simplify our record fine.
So here is one.
X is to an x value is unknown.
Let us see what happens to this SDR variable.
And here we get X is 1 in the output just because this condition is actually mashed.
Now suppose if I put the value of x as hundred.
So this condition as part will be executed because one is not matched with X and 2 is not matched with
X.
Now suppose if I print here let's see X is an alien right now in this situation what will happen because
we have to string expressions inside the Spart eggs value is unknown.
X is an alien.
So in this situation let's see what happens in the output console.
So the output.
So we are getting X is an alien.
Now what happens is that vortiger statement we have inside the code block at the end is actually return.
So X is an alien is actually returned and stored inside the SDR string variable.
And vortiger called that we have written here will actually be unused or it is actually read and then
called We are simply visiting them in body.
So here you can notice the expression is actually unused.
Right.
So always remember this whatever you write at the end of the quarter block is actually done and assigned
to the variable right.
So this is all about using an as expression here.
The when statements is actually acting as an expression and returning some value.
So in future forget about the switch statement and simply adapt to the an expression.
Thanks for watching and have a good day.