Welcome back.
This video we are going to look at Catlin's When Expression's, which is a replacement force, which
statements from other programming languages such as C, C++, C, Sharp Java and many others.
So if you have never seen any such 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 off those other programming
languages.
So let's go ahead and use it.
And therefore, I'm going to go ahead and put away our idea.
Already said, hey, why don't you use it when expression here?
So when we use this, if H so-and-so had such 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 E 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 or I'm going to set
the season two three.
And then I have a one expression Soyuzes when 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 a specific
code.
So let's say season is one, then I want to execute the code, which just says it's spring then of 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
to 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 and fall or print L.N. or in this case, both print L.N..
Autumn, and then, of course, we have our final season, which is winter.
All right, so pendelton 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 else block with the 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 expression's.
So you have a variable that you look at.
It 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 to use.
Also winter three, spring for 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 keyword 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.
Executes three dot dot five.
So in those months of 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 Brent Allen fall.
All right, but now what do we do with winter?
All right.
We cannot do something like in 12 dot dot 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 two, 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 arrange down, you can use a keyword down, too.
But what I want to do is actually have the values to 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 PRINDLE 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.
Already now a little challenge for you, please rebuild.
What we had here.
But do it with a one 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 that in key words.
So let's say in that range, if the values in the range don't please just go ahead and print.
You now may drink in the US and 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 in key word, actually, so let's get rid of those.
We can just say you may drive now and then finally, we have an LST 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 ink word and you just say, OK,
if it's none or not within zero to 20, then run this expression.
So what that will do 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 righty, so now you saw to use when expressions.
Now I want to show you one more thing about One Expression's, because we also can use a keyword, which
is called is so.
We have this variable 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 or thirteen point three 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 end.
But if it is a double, then please print, it's a double and then of course, of course, if it is
a string print and is a string and finally, we also have this plot here as well where we say, OK,
print 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's this thirteen point three, 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 expression's, you can, of course also use not is so you can say something like
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 is, thirteen
point three 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 Int and double for this whole
thing to be true.
So let's just say we want to make this a string and see if the 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 fact a string, because we added
two exclamation marks around it.
This is a string, in fact.
But this code will never be executed because one of the above is already executed or it follows the
same structure as we had when looking at if and if.
All right.
So it 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.