Hi everyone.
Welcome back.
In this lecture we are going to look into one another aspect that is part of the enhanced switch statement.
So in here what we have seen so far is that these are all single line case label body.
Right.
So the body is always single line.
Let's say for some reason you wanted to have some execution of different code statements and then return
a value.
What are the options we have when it comes to enhanced switch.
So in this case what we are going to do, we are going to have this February case label to have some
additional case body and then return this value.
Okay.
So what I'm going to do I'm going to come on this one out.
There we go.
And then what we are going to do.
So we are going to introduce a lambda over here.
So in the lambda we are going to have this open and close parenthesis.
So this is going to have some additional content for this specific case label.
So in this case what I'm going to do I'm going to print the value.
So the value is going to be I'm going to print the month.
So or you can add any logger you want.
So in this case maybe we can add check if here is and you can provide the year over here.
And then we can add is a leap year okay.
So this is to make sure we add some content to the case body.
So that this particular case body becomes a multi statement case body instead of a single line case
body okay.
So in this case what we are going to do I'm going to use the same functionality.
Right.
Our functionality is going to be pretty much the same.
But in order for this enhanced switch to support this kind of case body, what you need to do is you
need to use a new keyword.
It's going to be yield.
So if you use yield, yield is going to make sure it executes this particular statement.
And then what it is going to do it is going to execute this.
And once this is satisfied let's say the case month that's been passed to this specific function is
February.
So in this case it's going to execute this statement.
And then it's going to yield on this specific code statement.
And this is the return value of the specific switch expression.
So let's quickly test this one with the change we have made.
And we have February over here.
So this is to make sure the change that we made is not going to break anything.
So as long as we execute this particular test case and all the test conditions return green, then in
that case, our test case is working as expected.
And also the code change that we made is also working as expected.
So to quickly recap.
So anytime you have a case label which needs to have multiple code statements in it, in those kind
of scenarios, you have to have a lambda with a open and close curly braces, and then you provide whatever
statement you want to execute inside that lambda body.
But the last statement of this specific lambda body is going to be ending with yield.
So in the enhanced switch this is how you code your switch expression where the case block needs to
have multiple code statements in it.
I hope you all have a pretty good idea about how to use yield in our enhanced switch.
This marks the end of this lecture.
Thank you for watching.