1 00:00:01,290 --> 00:00:05,520 And now in this video we're going to check out then as the expression. 2 00:00:05,550 --> 00:00:11,700 Now this is actually the replacement of the switchgear statement that is found in C language or the 3 00:00:11,700 --> 00:00:12,940 Java language. 4 00:00:12,990 --> 00:00:16,320 So let us explore then as the expression as well. 5 00:00:16,410 --> 00:00:18,600 So insert the intelligent ID here. 6 00:00:18,600 --> 00:00:23,570 I have simply defined variable x equal to do and its value is final. 7 00:00:23,610 --> 00:00:31,200 Now instead of writing the switch statement that we used to do in case of let's say Java here we have 8 00:00:31,500 --> 00:00:33,020 the then statement. 9 00:00:33,270 --> 00:00:40,450 So when X does index is same good as phase and then inside the code block let us write our called. 10 00:00:40,500 --> 00:00:50,490 Lets see if X if the value of x is equal to one then simply put the dash followed by Arrow and then 11 00:00:50,670 --> 00:00:52,510 simply write the call here. 12 00:00:52,740 --> 00:00:56,660 But in line X is when. 13 00:00:56,730 --> 00:00:58,320 Right and now. 14 00:00:58,380 --> 00:01:04,720 Similarly if the value of x is equal to 2 then simply execute this line of code. 15 00:01:04,890 --> 00:01:14,670 Let's see what inline X is do right now here let us when the called so we get the output as X is 2. 16 00:01:15,120 --> 00:01:15,840 Now here. 17 00:01:15,840 --> 00:01:21,270 Notice we don't have to write any statement ordered the code in the new statement in case of switch 18 00:01:21,270 --> 00:01:22,300 case statement. 19 00:01:22,470 --> 00:01:28,600 Unlike in Java or C++ where we use to date E-Street Band bench every We're now similar to Java. 20 00:01:28,620 --> 00:01:32,510 We also have the forward statement in case of the end clause. 21 00:01:32,610 --> 00:01:39,480 So the forward statement into oven is actually represented with the help of is right. 22 00:01:39,480 --> 00:01:41,550 So we need to read as part here. 23 00:01:41,820 --> 00:01:45,470 And again this index is bash followed by arrow. 24 00:01:45,490 --> 00:01:51,210 Now are just print x value is unknown right. 25 00:01:51,360 --> 00:01:54,330 And now here I will simply change it to legacy 5. 26 00:01:54,360 --> 00:02:01,570 Let us note in the chord and in the output we have x value is unknown just because the x value is 5 27 00:02:01,630 --> 00:02:05,720 and we have just read in the code for 1 and 2 not 4 5. 28 00:02:05,740 --> 00:02:12,880 So the else part is executed and now we can also have the code block in case of inside the venue clause 29 00:02:12,940 --> 00:02:16,570 such as the block here and then the block here. 30 00:02:16,570 --> 00:02:23,880 Now inside which they can simply write our chords multiple lines of code such as print Elen x value 31 00:02:23,870 --> 00:02:25,120 is unknown. 32 00:02:25,570 --> 00:02:31,130 And also let us bring in I don't know what is x. 33 00:02:31,240 --> 00:02:33,910 Let us run the code. 34 00:02:34,050 --> 00:02:36,480 So here we get x value is unknown. 35 00:02:36,540 --> 00:02:39,660 I don't know what is x no similar to as far. 36 00:02:39,690 --> 00:02:42,070 You can also put the cord block here. 37 00:02:43,440 --> 00:02:44,090 Like this. 38 00:02:44,250 --> 00:02:50,670 And here again you can simply put the multiple lines of code inside this right like we have done in 39 00:02:50,670 --> 00:02:52,390 case of ex-parte here. 40 00:02:53,460 --> 00:02:58,180 And now suppose I have the value of let's say X equal to zero. 41 00:02:58,180 --> 00:03:04,540 Now here if you want to execute the medical condition in just one statement then what you can do is 42 00:03:04,750 --> 00:03:12,440 you can simply use 0 or more of an so here the x value is 0 or 1. 43 00:03:12,570 --> 00:03:13,040 Right. 44 00:03:13,060 --> 00:03:20,590 So you can combine multiple conditions here as well or you can also add three or four or something like 45 00:03:20,590 --> 00:03:20,900 that. 46 00:03:20,950 --> 00:03:24,800 But let's try it out with 0 and when. 47 00:03:24,850 --> 00:03:31,460 So in the output console we have X is either 0 or 1 like this. 48 00:03:31,530 --> 00:03:40,080 And now apart from this they can also use the range in case of values here let's say x is equal to 1. 49 00:03:40,170 --> 00:03:40,720 Right. 50 00:03:40,920 --> 00:03:48,500 And now suppose here if I use the range of when dot dot let's say 20 or let's change it to let's say 51 00:03:48,540 --> 00:03:49,160 11. 52 00:03:49,470 --> 00:03:54,700 And here I would simply use the in operator in space one dot dot Trente. 53 00:03:54,840 --> 00:04:01,710 So this range of values contains a value from 1 to 20 which also includes live in as well. 54 00:04:01,710 --> 00:04:06,770 So this condition will simply return true and this statement will be executed. 55 00:04:06,840 --> 00:04:09,480 So here we change the print statement. 56 00:04:10,270 --> 00:04:11,800 X lies in. 57 00:04:11,980 --> 00:04:21,180 Let's see one two twenty eight so in the outward and so we have x lies in one 220 now also you can use 58 00:04:21,360 --> 00:04:23,360 the not operator here. 59 00:04:23,370 --> 00:04:23,840 Fine. 60 00:04:23,910 --> 00:04:26,870 So the compiler and the condition becomes false here. 61 00:04:26,940 --> 00:04:29,590 They simply look for the match here. 62 00:04:29,610 --> 00:04:33,830 And again if we don't find a match then by default it will simply exit. 63 00:04:33,870 --> 00:04:35,320 That is part here. 64 00:04:35,340 --> 00:04:38,470 So now let us run the code. 65 00:04:38,610 --> 00:04:41,040 So here we get x value is unknown. 66 00:04:41,100 --> 00:04:42,930 I don't know what is X. 67 00:04:42,930 --> 00:04:43,760 Right. 68 00:04:43,800 --> 00:04:46,180 So you can simply use the range here. 69 00:04:46,200 --> 00:04:53,970 In case of when statement and now their topic of discussion for this video was actually then an expression. 70 00:04:53,980 --> 00:04:59,080 So suppose if I define a string Let's see that space SDR. 71 00:04:59,200 --> 00:05:01,630 Let's see it is of that type of string. 72 00:05:01,630 --> 00:05:06,620 And now if I want to print something here let's see if x is equal to 1. 73 00:05:06,640 --> 00:05:09,310 And here let's simplify the code as well. 74 00:05:09,630 --> 00:05:11,420 Print x is when. 75 00:05:11,440 --> 00:05:12,100 Right. 76 00:05:12,100 --> 00:05:19,120 So here what they really do is instead of printing some value I would simply assign SDR equal to legacy 77 00:05:19,180 --> 00:05:25,030 X is 1 and then again it's are equal to let's see X is do. 78 00:05:25,270 --> 00:05:33,070 And similarly in the as far as well I would simply assign SDR equal to x value is unknown and simply 79 00:05:33,070 --> 00:05:36,230 remove the last statement right. 80 00:05:36,600 --> 00:05:39,020 And at last I will simply print SDR. 81 00:05:39,480 --> 00:05:42,280 And now let us the called. 82 00:05:42,430 --> 00:05:45,690 So in the output we have X is when that is. 83 00:05:45,700 --> 00:05:50,960 This statement has been executed and the value of SDR becomes X is 1. 84 00:05:50,980 --> 00:05:56,260 Now in this way you can simply assign the values when some condition is actually matched. 85 00:05:56,260 --> 00:05:58,360 Now similar to the statement. 86 00:05:58,420 --> 00:06:01,250 You can also use the end statement as an expression. 87 00:06:01,360 --> 00:06:04,810 So in that case what you will do is simply quoted from here. 88 00:06:04,990 --> 00:06:12,820 Control X and then simply pasted just beside the main statement and then simply use that equally to 89 00:06:12,820 --> 00:06:13,730 operator. 90 00:06:14,020 --> 00:06:20,260 Now here what is happening is when this condition is actually matched then it will simply return this 91 00:06:20,350 --> 00:06:24,530 value and this value will be assigned to this SDR variable. 92 00:06:24,570 --> 00:06:25,190 Right. 93 00:06:25,270 --> 00:06:31,360 So let us remove the SDR variable from here and that does simplify our record fine. 94 00:06:31,360 --> 00:06:32,900 So here is one. 95 00:06:32,960 --> 00:06:35,530 X is to an x value is unknown. 96 00:06:35,560 --> 00:06:40,230 Let us see what happens to this SDR variable. 97 00:06:40,290 --> 00:06:45,870 And here we get X is 1 in the output just because this condition is actually mashed. 98 00:06:45,870 --> 00:06:49,430 Now suppose if I put the value of x as hundred. 99 00:06:49,470 --> 00:06:56,190 So this condition as part will be executed because one is not matched with X and 2 is not matched with 100 00:06:56,190 --> 00:06:56,820 X. 101 00:06:56,820 --> 00:07:05,490 Now suppose if I print here let's see X is an alien right now in this situation what will happen because 102 00:07:05,610 --> 00:07:10,510 we have to string expressions inside the Spart eggs value is unknown. 103 00:07:10,680 --> 00:07:13,080 X is an alien. 104 00:07:13,220 --> 00:07:16,710 So in this situation let's see what happens in the output console. 105 00:07:17,850 --> 00:07:18,880 So the output. 106 00:07:18,930 --> 00:07:21,660 So we are getting X is an alien. 107 00:07:21,660 --> 00:07:28,970 Now what happens is that vortiger statement we have inside the code block at the end is actually return. 108 00:07:29,100 --> 00:07:34,950 So X is an alien is actually returned and stored inside the SDR string variable. 109 00:07:36,000 --> 00:07:42,080 And vortiger called that we have written here will actually be unused or it is actually read and then 110 00:07:42,120 --> 00:07:44,540 called We are simply visiting them in body. 111 00:07:44,550 --> 00:07:48,030 So here you can notice the expression is actually unused. 112 00:07:48,090 --> 00:07:48,710 Right. 113 00:07:48,720 --> 00:07:55,410 So always remember this whatever you write at the end of the quarter block is actually done and assigned 114 00:07:55,530 --> 00:07:57,570 to the variable right. 115 00:07:57,660 --> 00:08:01,480 So this is all about using an as expression here. 116 00:08:01,610 --> 00:08:07,110 The when statements is actually acting as an expression and returning some value. 117 00:08:07,140 --> 00:08:13,260 So in future forget about the switch statement and simply adapt to the an expression. 118 00:08:13,260 --> 00:08:15,070 Thanks for watching and have a good day.