1 00:00:00,450 --> 00:00:03,510 ‫Some people say it's all about decisions in life. 2 00:00:03,930 --> 00:00:05,910 ‫I say it's all about decisions. 3 00:00:05,910 --> 00:00:08,170 ‫And C-sharp and that's what we're going to look at. 4 00:00:08,190 --> 00:00:16,260 ‫We're going to check out if statements and they help us to make decisions and run code based on those 5 00:00:16,260 --> 00:00:17,090 ‫decisions. 6 00:00:17,100 --> 00:00:20,820 ‫And what we do here is we have an if statement. 7 00:00:20,820 --> 00:00:26,970 ‫So this is a very basic if statement where we have a condition within curly brackets and then where 8 00:00:26,970 --> 00:00:28,680 ‫we have a condition within brackets. 9 00:00:28,680 --> 00:00:33,450 ‫And within the curly brackets, we have the code that should be run if the condition is met. 10 00:00:33,450 --> 00:00:39,780 ‫So let's say we check if the weather is good and if it's good, then we want to go outside. 11 00:00:39,780 --> 00:00:41,910 ‫And if it's bad weather, then we stay inside. 12 00:00:41,910 --> 00:00:46,710 ‫So the condition would be, is weather good or is the sun shining? 13 00:00:46,710 --> 00:00:53,850 ‫So that would be a very simple condition here where we simply compare if this is weather great or the 14 00:00:53,880 --> 00:00:57,990 ‫sun is shining or something like that is true, and if it's true, then run the code. 15 00:00:57,990 --> 00:01:00,150 ‫If it's not true, don't do anything. 16 00:01:00,150 --> 00:01:04,980 ‫So that's what this statement would do, but it can be a bit more complex. 17 00:01:04,980 --> 00:01:08,910 ‫So we can go ahead and have an IF statement with an LS statement. 18 00:01:08,910 --> 00:01:17,250 ‫So if condition is met, run some code and if it's not met, so else in any other case, go ahead and 19 00:01:17,250 --> 00:01:18,420 ‫run some other code. 20 00:01:18,420 --> 00:01:23,130 ‫So if none of the conditions is met, then the LS statement will be run. 21 00:01:23,400 --> 00:01:25,140 ‫And we have a prioritization here. 22 00:01:25,140 --> 00:01:29,940 ‫So if this first condition is true, this else will never be true and it will never be run. 23 00:01:30,180 --> 00:01:36,150 ‫And it's even more important if we check out the next step because we can have something called ls. 24 00:01:36,150 --> 00:01:39,870 ‫If so, if condition one what to do? 25 00:01:39,870 --> 00:01:44,340 ‫If condition is met else, then that's the default state. 26 00:01:44,340 --> 00:01:50,040 ‫So if none of the conditions is met and then we have a different aspect here to consider, which is 27 00:01:50,040 --> 00:01:55,590 ‫else if and we can have multiple ls if within one bigger if statement. 28 00:01:55,590 --> 00:02:03,060 ‫So we have if condition one what to do if condition is met then we have ls if condition two what to 29 00:02:03,060 --> 00:02:04,290 ‫do if condition two is met. 30 00:02:04,290 --> 00:02:07,710 ‫So the first condition could be Sun is shining. 31 00:02:07,710 --> 00:02:08,100 ‫All right. 32 00:02:08,100 --> 00:02:10,860 ‫If sun is not shining, it could be the sun is not shining. 33 00:02:10,860 --> 00:02:11,760 ‫Okay, we know that. 34 00:02:11,760 --> 00:02:13,170 ‫But then we check. 35 00:02:13,170 --> 00:02:19,260 ‫The second condition, for example, is the weather at least warm or is it at least plus 20 degrees 36 00:02:19,260 --> 00:02:20,070 ‫Celsius? 37 00:02:20,070 --> 00:02:23,460 ‫Then we would go outside still because we don't care. 38 00:02:23,460 --> 00:02:25,740 ‫Then we just put on the jacket and we go outside. 39 00:02:25,740 --> 00:02:27,270 ‫So that would be the second condition. 40 00:02:27,270 --> 00:02:33,130 ‫But if none of those are met, so if the sun is not shining and it's freezing outside, then we go into 41 00:02:33,130 --> 00:02:38,010 ‫the LS and we say, Oh, I'm going to stay at home, I'm going to drink a cup of tea and I'm just going 42 00:02:38,010 --> 00:02:39,300 ‫to chill in front of the TV. 43 00:02:39,300 --> 00:02:43,860 ‫So that would be an example of an if ls if on L statement. 44 00:02:44,220 --> 00:02:50,550 ‫So you've seen the syntax and it makes a lot more sense once we get into the demo. 45 00:02:50,550 --> 00:02:55,320 ‫So I'd say let's go ahead and have a quick demo where we simply are going to look at. 46 00:02:55,320 --> 00:03:00,180 ‫If so, we're not going to look at the other aspects we're just going to use if for now. 47 00:03:00,660 --> 00:03:00,960 ‫All right. 48 00:03:00,960 --> 00:03:04,770 ‫So let's go into C-sharp and into Visual Studio. 49 00:03:05,100 --> 00:03:05,520 ‫All right. 50 00:03:05,520 --> 00:03:07,290 ‫So that we are let's go ahead. 51 00:03:07,290 --> 00:03:12,990 ‫So I'm going to create a variable which is going to be an integer, and I'm going to say that that's 52 00:03:12,990 --> 00:03:15,000 ‫the temperature temperature. 53 00:03:15,300 --> 00:03:18,750 ‫And I'm going to set that to, let's say, ten degrees Celsius. 54 00:03:18,840 --> 00:03:26,820 ‫So now I want to check if the temperature is below ten degrees. 55 00:03:27,030 --> 00:03:30,390 ‫And if that's the case, I want to put on a code. 56 00:03:30,390 --> 00:03:40,440 ‫So I'm going to write onto the console right line, take the code. 57 00:03:41,970 --> 00:03:50,400 ‫Now, if the temperature is ten degrees, then I want to write something like 58 00:03:54,930 --> 00:03:57,450 ‫it's ten degrees 59 00:04:00,090 --> 00:04:03,150 ‫C, so ten degrees Celsius. 60 00:04:04,110 --> 00:04:17,850 ‫And now finally, if temperature is higher than ten, I just want to tell something like cozy, warm, 61 00:04:18,240 --> 00:04:25,740 ‫even though that's not really the case because it's pretty cold with ten degrees. 62 00:04:25,740 --> 00:04:27,630 ‫But still I just chose ten degrees. 63 00:04:27,630 --> 00:04:28,830 ‫So let's check it out. 64 00:04:28,830 --> 00:04:35,130 ‫By the way, we need to add the console, right, to get anything onto our console. 65 00:04:35,550 --> 00:04:38,100 ‫So let's do that and let's run our code. 66 00:04:38,850 --> 00:04:40,740 ‫And it's going to say it's ten degrees. 67 00:04:40,740 --> 00:04:41,490 ‫Why is that? 68 00:04:41,490 --> 00:04:44,010 ‫Because that condition is not met. 69 00:04:44,010 --> 00:04:46,110 ‫That one is not met, but this one is met. 70 00:04:46,230 --> 00:04:54,030 ‫So let's say we take a temperature of six degrees and we run the same code and it says, take the code. 71 00:04:54,960 --> 00:04:55,500 ‫All right. 72 00:04:55,500 --> 00:04:59,640 ‫Now, let's say we have a temperature of 25 degrees. 73 00:05:00,880 --> 00:05:02,920 ‫And it's going to say cozy and warm. 74 00:05:03,640 --> 00:05:04,270 ‫Great. 75 00:05:04,270 --> 00:05:07,350 ‫So now a little, little challenge for you. 76 00:05:07,360 --> 00:05:14,530 ‫Please edit the code so that it says or takes the weather into consideration and you can enter the weather 77 00:05:14,530 --> 00:05:15,460 ‫manually. 78 00:05:15,460 --> 00:05:22,960 ‫And it also is going to tell you if you should take the code, if you should go in shorts, or if you 79 00:05:22,960 --> 00:05:27,670 ‫should go like with a pullover and pants or something like that. 80 00:05:28,060 --> 00:05:28,510 ‫All right. 81 00:05:28,510 --> 00:05:34,540 ‫So just check that out and just change the code a little bit and then we will see the result. 82 00:05:34,540 --> 00:05:36,070 ‫So please go ahead and try that. 83 00:05:36,070 --> 00:05:41,140 ‫By the way, you should check out check the weather out outside and check the temperature and enter 84 00:05:41,140 --> 00:05:41,470 ‫that. 85 00:05:41,500 --> 00:05:45,550 ‫If you have a Fahrenheit temperature, then just use Fahrenheit values here. 86 00:05:45,550 --> 00:05:46,510 ‫That makes sense. 87 00:05:48,760 --> 00:05:49,240 ‫All right. 88 00:05:49,240 --> 00:05:50,620 ‫I hope you tried it. 89 00:05:51,040 --> 00:05:55,660 ‫And it's console dot red line what we need here. 90 00:05:56,050 --> 00:06:02,920 ‫So we're going to read in the line that the user has entered and it's going to be a string, of course. 91 00:06:02,920 --> 00:06:05,110 ‫So the temperature is going to be a string. 92 00:06:05,410 --> 00:06:11,170 ‫Now we need to check and change the temperature into an integer. 93 00:06:11,170 --> 00:06:13,840 ‫So int num temp. 94 00:06:14,320 --> 00:06:20,620 ‫So I'm going to call it and it's going to be in 32.4. 95 00:06:20,620 --> 00:06:33,490 ‫Actually int is good enough int parse temperature and now I can compare the num temp to let's say 20. 96 00:06:35,530 --> 00:06:43,360 ‫So if it's lower than 20, if it's just 20 or maybe even if it's higher than 20. 97 00:06:43,360 --> 00:06:46,600 ‫So this would be the right fit, I'd say. 98 00:06:46,990 --> 00:06:52,960 ‫All right, so what I want to change the text to is, well, if it's below 20, take the code. 99 00:06:52,960 --> 00:07:03,790 ‫If it's 20 degrees, then just write something like pants and pullover should be fine. 100 00:07:05,290 --> 00:07:09,790 ‫And then finally, if it's higher than 20 degrees of it's warmer then. 101 00:07:09,790 --> 00:07:10,060 ‫Right. 102 00:07:10,060 --> 00:07:14,080 ‫Something like shorts are enough today. 103 00:07:16,060 --> 00:07:18,460 ‫All right, so now let's check it out. 104 00:07:18,460 --> 00:07:21,910 ‫Let's see what our program has to tell us. 105 00:07:21,910 --> 00:07:25,180 ‫And I'm going to enter, let's say, 25. 106 00:07:25,390 --> 00:07:27,550 ‫And it says Shorts are enough today. 107 00:07:27,580 --> 00:07:31,570 ‫Maybe we should enter a little text here which tells us what to do. 108 00:07:31,570 --> 00:07:39,880 ‫So console, dot, right line, what's the temperature like? 109 00:07:41,710 --> 00:07:43,240 ‫And I'll start it again. 110 00:07:44,140 --> 00:07:45,400 ‫What's the temperature like? 111 00:07:45,400 --> 00:07:46,840 ‫I'm going to say, all right. 112 00:07:46,840 --> 00:07:48,910 ‫Well, outside it's eight degrees. 113 00:07:50,080 --> 00:07:51,160 ‫Take the code. 114 00:07:51,340 --> 00:07:51,760 ‫All right. 115 00:07:51,760 --> 00:07:52,720 ‫I'm going to take my code. 116 00:07:52,720 --> 00:07:53,260 ‫It's fine. 117 00:07:53,260 --> 00:07:54,250 ‫Thank you very much. 118 00:07:54,250 --> 00:08:00,670 ‫So now we have a little program which is going to help us to decide what to wear based on the temperature. 119 00:08:00,670 --> 00:08:01,480 ‫Isn't that great? 120 00:08:01,480 --> 00:08:07,060 ‫So we have our first little program which actually has used or which is useful. 121 00:08:07,060 --> 00:08:11,680 ‫Yeah, of course you can go ahead and extend that a little bit. 122 00:08:11,680 --> 00:08:19,480 ‫And while that does work, this if if if approach here it's not the cleanest and we will see how we 123 00:08:19,480 --> 00:08:25,600 ‫can clean that up in the next video, how to make it even better and more effective, because currently 124 00:08:25,900 --> 00:08:30,610 ‫it's running this code, then it's running that code and then is running that code. 125 00:08:30,610 --> 00:08:33,760 ‫But the better approach would be to check if that's true. 126 00:08:33,760 --> 00:08:35,950 ‫Well, then the other ones can't be true. 127 00:08:35,950 --> 00:08:39,520 ‫So don't even bother running that code. 128 00:08:39,610 --> 00:08:45,310 ‫I told you about this approach not being super efficient, and I'm going to show you how to make it 129 00:08:45,310 --> 00:08:52,840 ‫more efficient, because what we have seen is if else, if and finally else. 130 00:08:53,170 --> 00:08:53,530 ‫All right. 131 00:08:53,530 --> 00:08:55,900 ‫So you have seen that in the first video of this chapter. 132 00:08:55,900 --> 00:09:00,970 ‫And now let's change the code that we have here to fit that structure. 133 00:09:00,970 --> 00:09:05,770 ‫You can try it yourself so you can pause the video and try to fit that as well. 134 00:09:05,800 --> 00:09:08,470 ‫Otherwise, just take a look at how I do it. 135 00:09:10,090 --> 00:09:10,480 ‫All right. 136 00:09:10,480 --> 00:09:11,620 ‫I hope you tried it. 137 00:09:11,620 --> 00:09:18,760 ‫So we check if the temperature is below 20, and then if that is true, then that will never be true. 138 00:09:18,760 --> 00:09:25,690 ‫So we won't need that code in case that is true, because it will just run and it's just unnecessary 139 00:09:25,690 --> 00:09:27,400 ‫processing power that we need. 140 00:09:27,400 --> 00:09:37,240 ‫But if we use ls instead so else if for example, then this code will only be run if that code here 141 00:09:37,240 --> 00:09:40,540 ‫is not true, or if that condition here is not true. 142 00:09:40,630 --> 00:09:45,640 ‫So now, if the temperature is not 20 or below 20, then we run that code. 143 00:09:45,640 --> 00:09:52,570 ‫If it's not below 20, then we go to the next line and we check is the temperature exactly 20. 144 00:09:52,960 --> 00:09:59,470 ‫And then we could go ahead and say something like else if and check for the next line. 145 00:09:59,770 --> 00:10:03,370 ‫So if temperature is higher than 20, then do that. 146 00:10:03,970 --> 00:10:08,260 ‫But that again would not be the most efficient way while it would work. 147 00:10:08,290 --> 00:10:15,970 ‫A more efficient way is simply to just use less here, because if that condition is not met and that 148 00:10:15,970 --> 00:10:21,460 ‫condition is not met, then that condition will run or the LL statement will run. 149 00:10:21,460 --> 00:10:23,500 ‫Because in our case it's pretty simple. 150 00:10:23,500 --> 00:10:24,940 ‫There are only those three options. 151 00:10:24,940 --> 00:10:28,420 ‫It's either below 20, exactly 20 or higher than 20. 152 00:10:29,140 --> 00:10:34,120 ‫Of course, we could other we could add other conditions like else. 153 00:10:34,120 --> 00:10:43,400 ‫If num temp is equal to 30, we could add that and that would be fine as well. 154 00:10:43,420 --> 00:10:45,670 ‫Then we will still have the condition of ls. 155 00:10:45,670 --> 00:10:48,250 ‫That means that all the other code. 156 00:10:48,250 --> 00:10:51,850 ‫So 21 to 29 would not be covered. 157 00:10:51,850 --> 00:10:56,050 ‫They would be covered by this ls and everything higher than 31 would be covered by this. 158 00:10:56,050 --> 00:11:02,380 ‫LS So we could approach that way or approach it that way, and then we could also have this higher than 159 00:11:02,380 --> 00:11:03,030 ‫30. 160 00:11:03,040 --> 00:11:09,010 ‫So if the temperature is higher than 30, then this code will be run, which would be, for example, 161 00:11:09,700 --> 00:11:13,480 ‫it's hot or it's super hot. 162 00:11:14,470 --> 00:11:23,830 ‫And all the other situations are so between 21 and 30 with this L statement will be run. 163 00:11:24,040 --> 00:11:24,640 ‫All right. 164 00:11:24,640 --> 00:11:30,490 ‫So these are the different options that we have, and this is a more efficient way than the one that 165 00:11:30,490 --> 00:11:31,480 ‫we have just seen.