1
00:00:00,590 --> 00:00:01,370
Welcome back.

2
00:00:01,750 --> 00:00:07,250
This video we are going to look at Catlin's When Expression's, which is a replacement force, which

3
00:00:07,250 --> 00:00:13,310
statements from other programming languages such as C, C++, C, Sharp Java and many others.

4
00:00:13,550 --> 00:00:19,880
So if you have never seen any such statements yet, no worries, because it's something that is similar

5
00:00:19,880 --> 00:00:22,390
to what we are going to build in this video.

6
00:00:22,550 --> 00:00:26,360
And it's just what is used in other programming languages.

7
00:00:26,600 --> 00:00:32,330
But it's a more compact and more powerful tool than to switch statements off those other programming

8
00:00:32,330 --> 00:00:32,810
languages.

9
00:00:33,170 --> 00:00:35,110
So let's go ahead and use it.

10
00:00:35,120 --> 00:00:40,510
And therefore, I'm going to go ahead and put away our idea.

11
00:00:40,520 --> 00:00:44,370
Already said, hey, why don't you use it when expression here?

12
00:00:44,390 --> 00:00:51,860
So when we use this, if H so-and-so had such a better way, would be to simply use a when expression

13
00:00:51,860 --> 00:00:53,180
or simplify this expression.

14
00:00:53,480 --> 00:00:59,330
Of course I could do that directly here and the E would do everything for me, but I want to build one

15
00:00:59,570 --> 00:01:00,500
myself.

16
00:01:00,500 --> 00:01:03,800
So I want to build a when expressing myself.

17
00:01:03,800 --> 00:01:10,610
Therefore I create a new variable called season and it's going to be season three or I'm going to set

18
00:01:10,610 --> 00:01:11,600
the season two three.

19
00:01:11,990 --> 00:01:18,200
And then I have a one expression Soyuzes when keyword then the brackets and then the variable that I

20
00:01:18,200 --> 00:01:22,070
want to look at, because here I specifically look at a variable.

21
00:01:22,400 --> 00:01:29,720
Let's say I look at the season and depending on what the value of season is, I want to execute a specific

22
00:01:29,720 --> 00:01:30,050
code.

23
00:01:30,780 --> 00:01:40,250
So let's say season is one, then I want to execute the code, which just says it's spring then of season

24
00:01:40,250 --> 00:01:40,760
is two.

25
00:01:41,150 --> 00:01:45,350
I want to execute the code that is going to be summer.

26
00:01:46,040 --> 00:01:54,260
So you can see the structure is the value, then an empty space and then minus greater sign and afterwards

27
00:01:54,260 --> 00:01:55,940
to code that you want to execute.

28
00:01:56,420 --> 00:02:03,350
Now, of course, you can also go ahead and execute a multi-line code so it doesn't have to be a single

29
00:02:03,350 --> 00:02:03,920
line code.

30
00:02:04,850 --> 00:02:15,950
And here I'm just going to say print and fall or print L.N. or in this case, both print L.N..

31
00:02:17,870 --> 00:02:23,900
Autumn, and then, of course, we have our final season, which is winter.

32
00:02:24,170 --> 00:02:28,000
All right, so pendelton winter.

33
00:02:28,580 --> 00:02:30,020
So what is the default case?

34
00:02:30,200 --> 00:02:34,160
So the case that should be executed if none of the above is true.

35
00:02:34,170 --> 00:02:37,340
So if season is neither one nor two or three No.

36
00:02:37,340 --> 00:02:43,190
Four, well, then we can execute the else block with the keyword.

37
00:02:43,400 --> 00:02:43,900
All right.

38
00:02:43,910 --> 00:02:48,460
So here I'm just going to say invalid season.

39
00:02:49,280 --> 00:02:51,650
So at least on Earth, we only have four seasons.

40
00:02:51,650 --> 00:02:53,600
So let's say we were on another planet.

41
00:02:53,600 --> 00:02:57,210
Maybe there were more seasons, but these are all the seasons that we have.

42
00:02:57,620 --> 00:03:02,390
So this is in general how we go about using when expression's.

43
00:03:03,320 --> 00:03:05,960
So you have a variable that you look at.

44
00:03:06,230 --> 00:03:08,000
It doesn't have to be an integer.

45
00:03:08,000 --> 00:03:09,530
It could be a string as well.

46
00:03:09,950 --> 00:03:15,810
But we are looking at a variable and then depending on the value that we have, we execute some code.

47
00:03:16,680 --> 00:03:22,940
Now, the cool thing is we can go ahead and do a slightly different approach to this as well.

48
00:03:22,940 --> 00:03:27,670
So I'm just going to go ahead and say I have a month instead of a season.

49
00:03:28,040 --> 00:03:29,680
So here the month will also be three.

50
00:03:30,170 --> 00:03:38,330
But now when the month is one, then we, for example, have winter to use.

51
00:03:38,330 --> 00:03:42,080
Also winter three, spring for spring five for spring and so forth.

52
00:03:42,080 --> 00:03:45,530
So each season has three months.

53
00:03:46,460 --> 00:03:52,490
So now we can go ahead and do as we used to when keyword again in the brackets we say which variable

54
00:03:52,490 --> 00:03:58,370
we want to look at and then in the curly brackets, that's where our code, our execution code will

55
00:03:58,370 --> 00:04:01,030
be, where we check the different states.

56
00:04:01,070 --> 00:04:03,200
So let's say I want to.

57
00:04:04,600 --> 00:04:08,080
Executes three dot dot five.

58
00:04:08,440 --> 00:04:15,070
So in those months of months of three to five, I want to print.

59
00:04:17,510 --> 00:04:21,079
Spring, then in six.

60
00:04:22,650 --> 00:04:25,710
Until 8:00, I want to print.

61
00:04:27,760 --> 00:04:28,330
Summer.

62
00:04:30,350 --> 00:04:35,870
So you see a very similar approach, only that now we're not looking at just one value, but we're looking

63
00:04:35,870 --> 00:04:37,430
at a range of values here.

64
00:04:37,430 --> 00:04:39,340
The ranges from six to eight.

65
00:04:39,800 --> 00:04:45,950
And then, of course, we can also go ahead from nine to 11.

66
00:04:48,380 --> 00:04:52,010
So Brent Allen fall.

67
00:04:53,480 --> 00:04:56,930
All right, but now what do we do with winter?

68
00:04:57,810 --> 00:04:58,290
All right.

69
00:04:58,680 --> 00:05:03,240
We cannot do something like in 12 dot dot to.

70
00:05:05,440 --> 00:05:12,850
Parental and winter, because if you hover over it, it says, did you mean to use down to because it

71
00:05:12,850 --> 00:05:14,710
says this range is empty?

72
00:05:15,460 --> 00:05:21,730
Well, we could try down two, but now what it will do is it will go from 12 down to two.

73
00:05:22,210 --> 00:05:24,550
And we had those cases here already.

74
00:05:25,470 --> 00:05:29,960
You can see that if you want to go arrange down, you can use a keyword down, too.

75
00:05:30,390 --> 00:05:37,500
But what I want to do is actually have the values to one and two so I can do it like that.

76
00:05:37,710 --> 00:05:44,820
I could just say, OK, if those three values are true or if the month is one of those three values,

77
00:05:44,820 --> 00:05:47,850
then please go ahead and just print the long winter.

78
00:05:49,040 --> 00:05:57,470
And then, of course, our case, we can just use the same as we did before, so else PRINDLE An invalid

79
00:05:57,470 --> 00:06:00,260
season and now if we execute that.

80
00:06:01,670 --> 00:06:10,190
Then we will see its fall autumn, and then finally we have spring because month three is March and

81
00:06:10,190 --> 00:06:11,510
March will be spring.

82
00:06:13,360 --> 00:06:17,260
Already now a little challenge for you, please rebuild.

83
00:06:18,470 --> 00:06:19,550
What we had here.

84
00:06:21,000 --> 00:06:27,810
But do it with a one statement or one expression, but don't just use the autocomplete feature here,

85
00:06:27,820 --> 00:06:28,140
OK?

86
00:06:29,160 --> 00:06:29,610
All right.

87
00:06:29,620 --> 00:06:30,610
So I hope you tried it.

88
00:06:31,380 --> 00:06:36,570
So what we need to do is, of course, go ahead and use one page.

89
00:06:38,010 --> 00:06:46,070
And now we have two different cases, so let's say somebody is 21 and above, so we can use 21 dot dots,

90
00:06:46,080 --> 00:06:50,260
let's say 150, because no one is older than this age.

91
00:06:50,280 --> 00:06:52,580
Of course, we need to use that in key words.

92
00:06:52,580 --> 00:06:59,220
So let's say in that range, if the values in the range don't please just go ahead and print.

93
00:06:59,910 --> 00:07:05,760
You now may drink in the US and by drinking I mean water, of course.

94
00:07:06,360 --> 00:07:13,170
Then in 18 to 20, we can say something like, you may vote now.

95
00:07:17,120 --> 00:07:19,370
And 16 and 17.

96
00:07:21,220 --> 00:07:25,660
And here we don't need to in key word, actually, so let's get rid of those.

97
00:07:27,280 --> 00:07:36,860
We can just say you may drive now and then finally, we have an LST blog which says you're too young.

98
00:07:37,750 --> 00:07:42,810
So this is something that we can, of course, do, especially the one here at the top.

99
00:07:43,630 --> 00:07:46,420
But what if I don't want to write it like that?

100
00:07:46,420 --> 00:07:55,210
Because I just want to say if somebody is none of the below, so none of the less than 20 or 20 included?

101
00:07:55,840 --> 00:08:01,960
Well, what you can do is you can use this exclamation mark before the ink word and you just say, OK,

102
00:08:02,230 --> 00:08:09,820
if it's none or not within zero to 20, then run this expression.

103
00:08:10,090 --> 00:08:16,570
So what that will do is it says, all right, if the person is less than zero, so younger than zero

104
00:08:16,570 --> 00:08:20,710
or older than 20, then execute this code.

105
00:08:22,530 --> 00:08:25,800
All righty, so now you saw to use when expressions.

106
00:08:26,010 --> 00:08:32,520
Now I want to show you one more thing about One Expression's, because we also can use a keyword, which

107
00:08:32,520 --> 00:08:34,559
is called is so.

108
00:08:35,490 --> 00:08:41,490
We have this variable X, let's say, and it's of type any so I specifically say that it's of type any,

109
00:08:41,789 --> 00:08:45,340
which means that it's of type object and other programming languages.

110
00:08:45,340 --> 00:08:53,790
So if you know any other programming, language just means that this is going to be of any type of type.

111
00:08:53,850 --> 00:08:54,200
All right.

112
00:08:54,210 --> 00:08:55,010
It could be any type.

113
00:08:55,200 --> 00:08:56,600
That's pretty much what I'm saying here.

114
00:08:56,850 --> 00:09:03,180
And I'm just assigning a value or thirteen point three seven and now I can check.

115
00:09:04,420 --> 00:09:15,230
X for its type, so I can just say if it is an integer, then please print something like X is an end.

116
00:09:17,480 --> 00:09:27,110
But if it is a double, then please print, it's a double and then of course, of course, if it is

117
00:09:27,110 --> 00:09:38,870
a string print and is a string and finally, we also have this plot here as well where we say, OK,

118
00:09:39,290 --> 00:09:40,970
print and X.

119
00:09:42,510 --> 00:09:45,810
Is none of the above.

120
00:09:46,740 --> 00:09:52,890
OK, so this is something that you can also do using this is key word to check if it is of a certain

121
00:09:53,040 --> 00:09:58,980
type, that can be very useful because sometimes you get data and you don't know of what type it is

122
00:09:58,980 --> 00:10:05,310
and then you want to execute code based on the type that the data is so that the variable is that you

123
00:10:05,310 --> 00:10:05,830
are looking at.

124
00:10:06,510 --> 00:10:06,930
All right.

125
00:10:06,940 --> 00:10:09,270
So if we execute this.

126
00:10:11,690 --> 00:10:16,040
We can see that it's this thirteen point three, seven is a double.

127
00:10:16,990 --> 00:10:25,120
Of course, it is a double because there is a point or a dot there, so let's say we would add an F

128
00:10:25,120 --> 00:10:27,370
here, which makes this a float value.

129
00:10:28,240 --> 00:10:30,290
So instead of double now, it's going to be float.

130
00:10:30,370 --> 00:10:34,360
Let's see what we get because it should say is none of the above.

131
00:10:35,190 --> 00:10:40,830
So thirteen point thirty seven is none of the above, so it's neither a double nor a string nor an end

132
00:10:41,760 --> 00:10:47,540
while looking at one expression's, you can, of course also use not is so you can say something like

133
00:10:47,580 --> 00:10:52,260
not is double, then is not a double.

134
00:10:53,160 --> 00:11:01,890
All right, let's run this again just to see if this or else will be executed and there it is, thirteen

135
00:11:01,890 --> 00:11:07,080
point three seven is not a double because it is not of type double.

136
00:11:07,420 --> 00:11:08,910
It's of another type.

137
00:11:09,160 --> 00:11:12,180
But you can also see this is never printed because.

138
00:11:13,190 --> 00:11:20,240
The statement here is true, it's now could be of any type except for Int and double for this whole

139
00:11:20,240 --> 00:11:21,290
thing to be true.

140
00:11:21,410 --> 00:11:28,880
So let's just say we want to make this a string and see if the string here at the bottom will be executed.

141
00:11:29,210 --> 00:11:29,810
Let's see.

142
00:11:31,040 --> 00:11:36,030
If it says X or X is a string and it says X is not a double.

143
00:11:36,470 --> 00:11:39,050
So you see we have the same order here.

144
00:11:39,320 --> 00:11:43,910
It goes from top to bottom because this statement here is true, actually.

145
00:11:43,910 --> 00:11:46,480
This one here, this one and that one.

146
00:11:46,500 --> 00:11:52,560
So those following ones will never be executed, even though this is in fact a string, because we added

147
00:11:52,560 --> 00:11:54,130
two exclamation marks around it.

148
00:11:54,530 --> 00:11:55,760
This is a string, in fact.

149
00:11:56,720 --> 00:12:03,320
But this code will never be executed because one of the above is already executed or it follows the

150
00:12:03,320 --> 00:12:07,930
same structure as we had when looking at if and if.

151
00:12:08,210 --> 00:12:08,660
All right.

152
00:12:08,660 --> 00:12:10,310
So it always goes from top to bottom.

153
00:12:12,320 --> 00:12:20,060
So you see, when it's just an alternative for if, but sometimes it's more convenient and more precise

154
00:12:20,060 --> 00:12:20,930
and dense.

155
00:12:21,840 --> 00:12:31,140
And it just reads, when X is of certain value or type, then do something, so do whatever is after

156
00:12:31,170 --> 00:12:31,740
the arrow.

