1 00:00:00,570 --> 00:00:01,500 ‫Welcome back. 2 00:00:01,530 --> 00:00:09,090 ‫In this video, I will show you a shortcut for if statements and they are well enhanced if statements. 3 00:00:09,360 --> 00:00:15,780 ‫And what they can do is they can pretty much be a whole if statement it just in one row that's pretty 4 00:00:15,780 --> 00:00:23,100 ‫cool on one line and yeah so generally what you have is a condition then you have the question mark, 5 00:00:23,100 --> 00:00:30,300 ‫the first expression, colon, second expression, and then of course you end the statement with a semicolon. 6 00:00:30,420 --> 00:00:37,650 ‫So that's generally how you can create a shortcut of an if statement or an enhanced if statement. 7 00:00:37,800 --> 00:00:43,020 ‫So some points to consider here are the condition has to be either true or false. 8 00:00:43,020 --> 00:00:45,630 ‫The conditional operator is right. 9 00:00:45,630 --> 00:00:51,510 ‫So it's associative, which means that if we have a look at this expression here, well, actually, 10 00:00:51,510 --> 00:00:57,390 ‫let's have a look at that expression once we created our first little if statement. 11 00:00:57,390 --> 00:01:00,390 ‫So let's first create some variables here. 12 00:01:00,420 --> 00:01:08,130 ‫Temperature is going to be, let's say, minus five and we have a string and I'm calling that state 13 00:01:08,130 --> 00:01:09,420 ‫of matter. 14 00:01:09,990 --> 00:01:13,380 ‫So I'm going to leave it empty for now. 15 00:01:13,380 --> 00:01:23,700 ‫And if let's say the temperature is below zero, then I want the state of matter to be solid. 16 00:01:24,030 --> 00:01:27,510 ‫So state of matter is going to be solid. 17 00:01:28,620 --> 00:01:29,190 ‫All right. 18 00:01:29,340 --> 00:01:30,330 ‫As simple as that. 19 00:01:30,480 --> 00:01:38,370 ‫And else I want it to be state of matter is going to be liquid. 20 00:01:40,170 --> 00:01:41,730 ‫So temperature is zero. 21 00:01:41,730 --> 00:01:43,650 ‫But this I mean, Celsius. 22 00:01:43,650 --> 00:01:49,740 ‫So in Celsius and a zero degree water freezes. 23 00:01:49,740 --> 00:01:52,110 ‫That's why it was set to zero degree. 24 00:01:52,110 --> 00:01:54,390 ‫So that's why we start with a zero. 25 00:01:54,390 --> 00:01:59,040 ‫That's when water starts to freeze or it starts to become liquid. 26 00:01:59,040 --> 00:02:01,290 ‫So that's pretty much the turning point. 27 00:02:01,980 --> 00:02:02,550 ‫All right. 28 00:02:02,550 --> 00:02:07,860 ‫So the next thing that we want to do is simply write that on to the console. 29 00:02:07,860 --> 00:02:19,920 ‫So CW State of Matter is and then I'm going to position that into those curly brackets as the state 30 00:02:19,920 --> 00:02:21,000 ‫of matter. 31 00:02:22,320 --> 00:02:30,210 ‫So now if we check that, we also need to console read key here so that we see what's actually true 32 00:02:30,210 --> 00:02:37,260 ‫and let's run this code and then we are the state of matter is solid, so that means the temperature 33 00:02:37,260 --> 00:02:38,790 ‫is below zero. 34 00:02:38,790 --> 00:02:41,520 ‫Okay, that's the general statement that we had. 35 00:02:41,550 --> 00:02:45,840 ‫Of course, you could sum it up and you can make it even shorter. 36 00:02:45,840 --> 00:02:52,530 ‫So let's say we have a situation like this, you can do it like that. 37 00:02:52,530 --> 00:02:58,140 ‫So then you have two lines of code and that's very short, but that's usually not how you would create 38 00:02:58,140 --> 00:03:00,750 ‫an if else statement. 39 00:03:00,750 --> 00:03:08,520 ‫And the better way, of course, is to warn the better, but the shorter way is to use a enhanced if 40 00:03:08,520 --> 00:03:09,150 ‫statement. 41 00:03:09,150 --> 00:03:11,700 ‫So let's go ahead and do just that. 42 00:03:11,970 --> 00:03:19,620 ‫So I'm just going to create the whole thing in short, which means we have the state of matter is going 43 00:03:19,620 --> 00:03:20,340 ‫to be. 44 00:03:21,650 --> 00:03:22,550 ‫Temperature 45 00:03:25,250 --> 00:03:26,240 ‫below zero. 46 00:03:26,540 --> 00:03:29,660 ‫If that's true, then do the first. 47 00:03:30,590 --> 00:03:32,510 ‫If it's false to the second. 48 00:03:34,640 --> 00:03:35,330 ‫That's it. 49 00:03:35,810 --> 00:03:41,020 ‫So the whole thing that we had here in four lines, we could simply do in just one line. 50 00:03:41,030 --> 00:03:44,360 ‫So that's an enhanced if statement. 51 00:03:45,800 --> 00:03:55,430 ‫Now, if we get rid of all of that up to here and now, we run the code again. 52 00:03:56,450 --> 00:03:58,910 ‫So I'm just going to run it again. 53 00:04:00,140 --> 00:04:03,260 ‫And we see state of matter is solid. 54 00:04:04,100 --> 00:04:12,260 ‫And actually, let me uncomment that and simply increase the temperature from that point to the other 55 00:04:12,470 --> 00:04:15,350 ‫by 30 degrees. 56 00:04:15,800 --> 00:04:21,510 ‫So now it's going to be warm, 25 degrees and water is liquid at that point. 57 00:04:21,530 --> 00:04:25,190 ‫So now we have our old if statement and then we have the short one. 58 00:04:25,190 --> 00:04:30,200 ‫And now if we run that, we once have solid and once we have liquid. 59 00:04:30,830 --> 00:04:31,460 ‫All right. 60 00:04:31,760 --> 00:04:39,770 ‫So that's generally how you can go ahead and create a very simple version of. 61 00:04:40,530 --> 00:04:44,790 ‫An enhanced if statement now for you is a little challenge. 62 00:04:45,390 --> 00:04:50,390 ‫So the challenge is add the gas state of matter to the options. 63 00:04:50,400 --> 00:04:59,730 ‫So now use again an enhanced if statement to add well pretty much the gas state to it. 64 00:04:59,730 --> 00:05:03,420 ‫So one temperature is higher than 100 degrees. 65 00:05:05,950 --> 00:05:06,310 ‫All right. 66 00:05:06,310 --> 00:05:07,180 ‫I hope you tried it. 67 00:05:07,180 --> 00:05:10,990 ‫I hope you tried it in a similar way that we have seen here. 68 00:05:11,200 --> 00:05:14,020 ‫I haven't touched it yet, but I will do that in a second. 69 00:05:14,020 --> 00:05:18,460 ‫So let's go ahead and create the following. 70 00:05:18,460 --> 00:05:23,050 ‫We check the temperature once again and now we say, okay, is it higher than 100? 71 00:05:23,080 --> 00:05:26,050 ‫If that's true, then it's gas. 72 00:05:27,540 --> 00:05:34,500 ‫But if it's not true, then we want to check the temperature once again and we want to see if it's below 73 00:05:34,500 --> 00:05:38,550 ‫zero, because if it is below zero, then it's solid. 74 00:05:38,550 --> 00:05:43,170 ‫And if it's not below zero, then it's liquid. 75 00:05:44,700 --> 00:05:50,850 ‫So now we have a whole else, if and so forth, statement in one line. 76 00:05:51,570 --> 00:05:59,110 ‫So now if I run that and I increase the temperature by 100 degrees, temperature plus equal 100. 77 00:05:59,130 --> 00:06:01,380 ‫So now it's going to be 130. 78 00:06:01,380 --> 00:06:03,650 ‫And it should evaporate. 79 00:06:03,660 --> 00:06:04,950 ‫It should become gas. 80 00:06:05,520 --> 00:06:10,140 ‫So now we should get the information that it is gas. 81 00:06:10,740 --> 00:06:12,290 ‫State of matter, solid. 82 00:06:12,300 --> 00:06:13,350 ‫That was the first one. 83 00:06:13,350 --> 00:06:14,880 ‫And now state of matter gas. 84 00:06:14,880 --> 00:06:17,130 ‫That's the one that we have here. 85 00:06:18,120 --> 00:06:18,870 ‫All right. 86 00:06:18,870 --> 00:06:20,670 ‫So what is important here? 87 00:06:21,330 --> 00:06:23,250 ‫What we have is we check. 88 00:06:24,410 --> 00:06:27,830 ‫If the temperature is higher than 100. 89 00:06:27,890 --> 00:06:35,350 ‫If that is true, then we run this code here, or we simply use gas as the state of matter. 90 00:06:35,360 --> 00:06:38,570 ‫But if it's not, then we have a second check. 91 00:06:38,570 --> 00:06:39,650 ‫So we check. 92 00:06:40,320 --> 00:06:41,490 ‫What is going on here? 93 00:06:41,940 --> 00:06:43,600 ‫Is the temperature below zero? 94 00:06:43,620 --> 00:06:45,900 ‫And if it is, yes. 95 00:06:45,930 --> 00:06:48,480 ‫If it's true, then the first entry is true. 96 00:06:48,510 --> 00:06:49,980 ‫Otherwise, the second one. 97 00:06:50,280 --> 00:06:52,890 ‫So we don't check it like this here. 98 00:06:53,550 --> 00:06:54,780 ‫We don't say, okay. 99 00:06:55,230 --> 00:06:56,640 ‫We check all of that. 100 00:06:58,410 --> 00:07:01,530 ‫If that is true, that whole statement, then it's solid. 101 00:07:01,560 --> 00:07:02,900 ‫Otherwise, it's liquid. 102 00:07:02,910 --> 00:07:04,000 ‫That's not what we're doing. 103 00:07:04,020 --> 00:07:08,130 ‫It's really important that you understand that what we are doing is we check. 104 00:07:09,180 --> 00:07:10,080 ‫The first. 105 00:07:10,380 --> 00:07:12,420 ‫Actually, we can do it like that. 106 00:07:12,630 --> 00:07:17,100 ‫We could, of course, see it in a way that we have this statement that we check. 107 00:07:17,100 --> 00:07:19,590 ‫But first we have this statement here, so we check. 108 00:07:19,920 --> 00:07:21,280 ‫Is this condition true? 109 00:07:21,300 --> 00:07:22,260 ‫Then we run that. 110 00:07:22,260 --> 00:07:24,690 ‫Otherwise we run this whole thing here. 111 00:07:24,990 --> 00:07:27,030 ‫What is inside of the brackets? 112 00:07:28,170 --> 00:07:29,730 ‫But that's not what you need to write. 113 00:07:29,730 --> 00:07:31,740 ‫You can simply leave it like that. 114 00:07:31,830 --> 00:07:33,990 ‫And that's what this statement here is saying. 115 00:07:34,080 --> 00:07:41,240 ‫So the expression A, B, C, D, E, that's what we have at the bottom is evaluated as is a true. 116 00:07:41,250 --> 00:07:48,150 ‫If yes, then B otherwise check is C true, then D otherwise e. 117 00:07:49,290 --> 00:07:51,300 ‫So that's pretty much what we have done. 118 00:07:51,300 --> 00:07:53,820 ‫And it's important that it's not like you see here. 119 00:07:53,820 --> 00:08:03,270 ‫So is a true then B or C, and if that whole statement is true, then D or E, that's not how we do 120 00:08:03,270 --> 00:08:03,390 ‫it. 121 00:08:04,470 --> 00:08:08,040 ‫And finally, the conditional operator cannot be overloaded. 122 00:08:08,070 --> 00:08:08,520 ‫All right. 123 00:08:08,520 --> 00:08:11,040 ‫So that's generally the enhanced F statement. 124 00:08:11,040 --> 00:08:19,350 ‫I hope you could manage to solve this challenge, and it will come in handy whenever you want to use 125 00:08:19,350 --> 00:08:21,750 ‫very simple and short sentences. 126 00:08:21,750 --> 00:08:28,200 ‫By the way, if we would now change our, if else, statement to what we have here at the bottom, it 127 00:08:28,200 --> 00:08:33,180 ‫would look like this else if temperature is higher than 100. 128 00:08:33,720 --> 00:08:40,770 ‫And finally else we have the state of matter is going to be. 129 00:08:42,830 --> 00:08:43,070 ‫Here. 130 00:08:43,070 --> 00:08:48,710 ‫It's going to be liquid, by the way, and this one here would be gas. 131 00:08:49,370 --> 00:08:51,530 ‫So it's not exactly the same approach. 132 00:08:51,530 --> 00:08:57,230 ‫I just want to show you that we need six lines here and that's even without using the curly brackets. 133 00:08:57,380 --> 00:09:02,090 ‫Or we just have one line where we handle all of the same code.