WEBVTT

00:00.140 --> 00:00.770
Hi everyone.

00:00.770 --> 00:01.370
Welcome back.

00:01.370 --> 00:06.530
In this lecture we are going to look into one another aspect that is part of the enhanced switch statement.

00:06.530 --> 00:12.230
So in here what we have seen so far is that these are all single line case label body.

00:12.260 --> 00:12.560
Right.

00:12.590 --> 00:14.510
So the body is always single line.

00:14.510 --> 00:20.660
Let's say for some reason you wanted to have some execution of different code statements and then return

00:20.660 --> 00:21.200
a value.

00:21.230 --> 00:23.990
What are the options we have when it comes to enhanced switch.

00:23.990 --> 00:29.600
So in this case what we are going to do, we are going to have this February case label to have some

00:29.630 --> 00:32.720
additional case body and then return this value.

00:32.750 --> 00:33.050
Okay.

00:33.080 --> 00:35.600
So what I'm going to do I'm going to come on this one out.

00:35.630 --> 00:36.200
There we go.

00:36.230 --> 00:37.490
And then what we are going to do.

00:37.520 --> 00:40.340
So we are going to introduce a lambda over here.

00:40.340 --> 00:43.850
So in the lambda we are going to have this open and close parenthesis.

00:43.850 --> 00:48.200
So this is going to have some additional content for this specific case label.

00:48.230 --> 00:51.740
So in this case what I'm going to do I'm going to print the value.

00:51.740 --> 00:54.110
So the value is going to be I'm going to print the month.

00:54.110 --> 00:56.840
So or you can add any logger you want.

00:56.870 --> 01:03.050
So in this case maybe we can add check if here is and you can provide the year over here.

01:03.530 --> 01:07.580
And then we can add is a leap year okay.

01:07.610 --> 01:11.630
So this is to make sure we add some content to the case body.

01:11.630 --> 01:18.320
So that this particular case body becomes a multi statement case body instead of a single line case

01:18.320 --> 01:19.070
body okay.

01:19.100 --> 01:23.060
So in this case what we are going to do I'm going to use the same functionality.

01:23.090 --> 01:23.420
Right.

01:23.450 --> 01:25.490
Our functionality is going to be pretty much the same.

01:25.490 --> 01:30.440
But in order for this enhanced switch to support this kind of case body, what you need to do is you

01:30.440 --> 01:32.360
need to use a new keyword.

01:32.390 --> 01:33.410
It's going to be yield.

01:33.410 --> 01:38.420
So if you use yield, yield is going to make sure it executes this particular statement.

01:38.420 --> 01:41.390
And then what it is going to do it is going to execute this.

01:41.390 --> 01:46.760
And once this is satisfied let's say the case month that's been passed to this specific function is

01:46.790 --> 01:47.420
February.

01:47.420 --> 01:49.670
So in this case it's going to execute this statement.

01:49.670 --> 01:52.820
And then it's going to yield on this specific code statement.

01:52.820 --> 01:56.180
And this is the return value of the specific switch expression.

01:56.180 --> 01:58.880
So let's quickly test this one with the change we have made.

01:58.880 --> 02:00.830
And we have February over here.

02:00.830 --> 02:05.000
So this is to make sure the change that we made is not going to break anything.

02:05.000 --> 02:11.210
So as long as we execute this particular test case and all the test conditions return green, then in

02:11.210 --> 02:13.520
that case, our test case is working as expected.

02:13.520 --> 02:17.270
And also the code change that we made is also working as expected.

02:17.270 --> 02:18.440
So to quickly recap.

02:18.470 --> 02:24.020
So anytime you have a case label which needs to have multiple code statements in it, in those kind

02:24.050 --> 02:29.690
of scenarios, you have to have a lambda with a open and close curly braces, and then you provide whatever

02:29.690 --> 02:32.360
statement you want to execute inside that lambda body.

02:32.360 --> 02:37.340
But the last statement of this specific lambda body is going to be ending with yield.

02:37.340 --> 02:43.310
So in the enhanced switch this is how you code your switch expression where the case block needs to

02:43.340 --> 02:45.590
have multiple code statements in it.

02:45.620 --> 02:50.120
I hope you all have a pretty good idea about how to use yield in our enhanced switch.

02:50.120 --> 02:51.560
This marks the end of this lecture.

02:51.560 --> 02:52.670
Thank you for watching.
