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