Welcome back.
This video, we are going to look at Collins when expressions, which is a replacement force, which
statements from other programming languages such as C, C++, E Sharp Java and many others.
So if you have never seen any switch statements yet, no worries because it's something that is similar
to what we are going to build in this video.
And it's just what is used in other programming languages, but it's a more compact and more powerful
tool than to switch statements of those other programming languages.
So let's go ahead and use it, and therefore I'm going to go ahead.
And by the way, our IEEE already said, Hey, why don't you use it when expression here?
So when we use this if age?
So and so it's it's a better way would be to simply use a when expression or simplify this expression.
Of course, I could do that directly here.
And the idea would do everything for me, but I want to build one myself, so I want to build a when
expressing myself.
Therefore, I create a new variable called Season and it's going to be season three.
I I'm going to set the season to three and then I have a one expression, so I use this one keyword.
Then the brackets and then the variable that I want to look at, because here I specifically look at
a variable.
Let's say I look at the season and depending on what the value of season is, I want to execute specific
code.
So let's say a season is one, then I want to execute the code, which just says it's spring.
Then if season is two, I want to execute the code that is going to be summer.
So you can see the structure is the value, then an empty space and then minus greater sign and afterwards
the code that you want to execute.
Now, of course, you can also go ahead and execute a multi line code, so it doesn't have to be a single
line code.
And here I'm just going to say print out and fall or print alone or in this case, both print and.
Autumn, and then of course, we have our final season, which is winter.
All right.
So print, Alan Winter.
So what is the default case?
So the case that should be executed if none of the above is true.
So if season is neither one nor two or three, No.
Four, well, then we can execute the Owls blog with the Owls keyword.
All right.
So here I'm just going to say invalid season.
So at least on Earth, we only have four seasons.
So let's say we were on another planet.
Maybe there were more seasons.
But these are all the seasons that we have.
So this is in general how we go about using when expressions.
So you have a variable that you look at doesn't have to be an integer.
It could be a string as well.
But we are looking at a variable.
And then depending on the value that we have, we execute some code.
Now the cool thing is we can go ahead and do a slightly different approach to this as well.
So I'm just going to go ahead and say I have a month instead of a season.
So here the month will also be three.
But now when the month is one, then we, for example, have winter.
Two is also winter, three spring for the spring, five for spring and so forth.
So each season has three months.
So now we can go ahead and do as we used to when you were again in the brackets, we say which variable
we want to look at.
And then in the curly brackets, that's where our code, our execution code will be, where we check
the different states.
So let's say I want to.
Execute three dot dot five.
So in those months or months of three to five, I want to print.
Spring, then in six.
Until 8:00, I want to print.
Summer.
So you see a very similar approach, only that now we're not looking at just one value, but we're looking
at a range of values here.
The ranges from six to eight.
And then, of course, we can also go ahead from nine to 11.
So Print Island Fall.
All right, but now what do we do with winter?
All right.
We cannot do something like in 12 days start to.
Parental and winter, because if you hover over it, it says, did you mean to use down to?
Because it says this range is empty?
Well, we could try down too.
But now what it will do is it will go from 12 down to two.
And we had those cases here already.
You can see that if you want to go a range down, you can use the keyboard down to.
But what I want to do is actually have the values 12, one and two, so I can do it like that.
I could just say, OK, if those three values are true or if the month is one of those three values,
then please go ahead and just print the long winter.
And then, of course, our case, we can just use the same as we did before, so else, print of an
invalid season.
And now if we execute that?
Then we will see its fall autumn, and then finally, we have spring because month three is March and
March will be spring.
All right.
Now a little challenge for you.
Please rebuild.
What we had here.
But do it with a weekend statement or one expression, but don't just use the autocomplete feature here,
OK?
All right, so I hope you tried it.
So what we need to do is, of course, go ahead and use one page.
And now we have two different cases, so let's say somebody is 21 and above, so we can use 21 dot dots,
let's say 150, because no one is older than this age.
Of course, we need to use two in key words.
So let's say in that range, if the values in that range don't, please just go ahead and print.
You now may drink in the U.S. by drinking.
I mean water, of course.
Then in 18 to 20, we can say something like You may vote now.
And 16 and 17.
And here we don't need to key, actually.
So let's get rid of those.
We can just say you may drive now.
And then finally, we have an ALS blog which says you're too young.
So this is something that we can of course do, especially the one here at the top.
But what if I don't want to write it like that?
Because I just want to say if somebody is none of the below, so none of the less than 20 or 20 included?
Well, what you can do is you can use this exclamation mark before the inky word and you just say,
OK, if it's none or not within zero to 20, then run this expression.
So what that will adjust to is it says, all right, if the person is less than zero.
So younger than zero or older than 20, then execute this code.
All right.
So now you saw to use when expressions.
Now I want to show you one more thing about when expressions because we also can use a keyword which
is called is so.
We have this verbal x, let's say, and it's of type any, so I specifically say that it's of type any,
which means that it's of type, object and other programming languages.
So if you know any other programming language just means that this is going to be of any type of type.
All right.
It could be any type.
That's pretty much what I'm saying here, and I'm just assigning a value of thirteen point thirty seven.
And now I can check.
X for its type, so I can just say, if it is an integer, then please print something like X is an
int.
But if it is a double, then please print.
It's a double.
And then of course, of course, if it is a string printer and is a string.
And finally, we also have this block here as well where we say, OK, print L and X.
Is none of the above.
OK, so this is something that you can also do using this is key word to check if it is of a certain
type that can be very useful because sometimes you get data and you don't know of what type it is and
then you want to execute code based on the type that the data is so that the variable is that you are
looking at.
All right.
So if we execute this?
We can see that it says thirteen point thirty seven is a double.
Of course, it is a double because there is a point or a dot there, so let's say we would add an f
here, which makes this a float value.
So instead of double now it's going to be float.
Let's see what we get because it should say is none of the above.
So thirteen point thirty seven is none of the above, so it's neither a double nor a string nor an end
while looking at one expressions, you can of course also use not is.
So you can say something or not is double then is not a double.
All right, let's run this again, just to see if this or else will be executed.
And there it says thirteen point thirty seven is not a double because it is not of type double.
It's of another type.
But you can also see this is never printed because.
The statement here is true.
It's now could be of any type, except for end and double for this whole thing to be true.
So let's just say we want to make this a string and see if this string here at the bottom will be executed.
Let's see.
If it says X or X is a string and it says X is not a double, so you see we have the same order here,
it goes from top to bottom because this statement here is true.
Actually, this one here, this one and that one, so those following ones will never be executed,
even though this is in effect, a string because we added the exclamation marks around it.
This is this string, in fact, but this code will never be executed because one of the above is already
executed.
All right.
So for this, the same structure as we had when looking at if and else if all right, so always goes
from top to bottom.
So you see, when it's just an alternative for if but sometimes it's more convenient and more precise
and dense.
And it just reads when X is of certain value or type, then do something.
So do whatever is after the arrow.