‫Some people say it's all about decisions in life.
‫I say it's all about decisions.
‫And C-sharp and that's what we're going to look at.
‫We're going to check out if statements and they help us to make decisions and run code based on those
‫decisions.
‫And what we do here is we have an if statement.
‫So this is a very basic if statement where we have a condition within curly brackets and then where
‫we have a condition within brackets.
‫And within the curly brackets, we have the code that should be run if the condition is met.
‫So let's say we check if the weather is good and if it's good, then we want to go outside.
‫And if it's bad weather, then we stay inside.
‫So the condition would be, is weather good or is the sun shining?
‫So that would be a very simple condition here where we simply compare if this is weather great or the
‫sun is shining or something like that is true, and if it's true, then run the code.
‫If it's not true, don't do anything.
‫So that's what this statement would do, but it can be a bit more complex.
‫So we can go ahead and have an IF statement with an LS statement.
‫So if condition is met, run some code and if it's not met, so else in any other case, go ahead and
‫run some other code.
‫So if none of the conditions is met, then the LS statement will be run.
‫And we have a prioritization here.
‫So if this first condition is true, this else will never be true and it will never be run.
‫And it's even more important if we check out the next step because we can have something called ls.
‫If so, if condition one what to do?
‫If condition is met else, then that's the default state.
‫So if none of the conditions is met and then we have a different aspect here to consider, which is
‫else if and we can have multiple ls if within one bigger if statement.
‫So we have if condition one what to do if condition is met then we have ls if condition two what to
‫do if condition two is met.
‫So the first condition could be Sun is shining.
‫All right.
‫If sun is not shining, it could be the sun is not shining.
‫Okay, we know that.
‫But then we check.
‫The second condition, for example, is the weather at least warm or is it at least plus 20 degrees
‫Celsius?
‫Then we would go outside still because we don't care.
‫Then we just put on the jacket and we go outside.
‫So that would be the second condition.
‫But if none of those are met, so if the sun is not shining and it's freezing outside, then we go into
‫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
‫to chill in front of the TV.
‫So that would be an example of an if ls if on L statement.
‫So you've seen the syntax and it makes a lot more sense once we get into the demo.
‫So I'd say let's go ahead and have a quick demo where we simply are going to look at.
‫If so, we're not going to look at the other aspects we're just going to use if for now.
‫All right.
‫So let's go into C-sharp and into Visual Studio.
‫All right.
‫So that we are let's go ahead.
‫So I'm going to create a variable which is going to be an integer, and I'm going to say that that's
‫the temperature temperature.
‫And I'm going to set that to, let's say, ten degrees Celsius.
‫So now I want to check if the temperature is below ten degrees.
‫And if that's the case, I want to put on a code.
‫So I'm going to write onto the console right line, take the code.
‫Now, if the temperature is ten degrees, then I want to write something like
‫it's ten degrees
‫C, so ten degrees Celsius.
‫And now finally, if temperature is higher than ten, I just want to tell something like cozy, warm,
‫even though that's not really the case because it's pretty cold with ten degrees.
‫But still I just chose ten degrees.
‫So let's check it out.
‫By the way, we need to add the console, right, to get anything onto our console.
‫So let's do that and let's run our code.
‫And it's going to say it's ten degrees.
‫Why is that?
‫Because that condition is not met.
‫That one is not met, but this one is met.
‫So let's say we take a temperature of six degrees and we run the same code and it says, take the code.
‫All right.
‫Now, let's say we have a temperature of 25 degrees.
‫And it's going to say cozy and warm.
‫Great.
‫So now a little, little challenge for you.
‫Please edit the code so that it says or takes the weather into consideration and you can enter the weather
‫manually.
‫And it also is going to tell you if you should take the code, if you should go in shorts, or if you
‫should go like with a pullover and pants or something like that.
‫All right.
‫So just check that out and just change the code a little bit and then we will see the result.
‫So please go ahead and try that.
‫By the way, you should check out check the weather out outside and check the temperature and enter
‫that.
‫If you have a Fahrenheit temperature, then just use Fahrenheit values here.
‫That makes sense.
‫All right.
‫I hope you tried it.
‫And it's console dot red line what we need here.
‫So we're going to read in the line that the user has entered and it's going to be a string, of course.
‫So the temperature is going to be a string.
‫Now we need to check and change the temperature into an integer.
‫So int num temp.
‫So I'm going to call it and it's going to be in 32.4.
‫Actually int is good enough int parse temperature and now I can compare the num temp to let's say 20.
‫So if it's lower than 20, if it's just 20 or maybe even if it's higher than 20.
‫So this would be the right fit, I'd say.
‫All right, so what I want to change the text to is, well, if it's below 20, take the code.
‫If it's 20 degrees, then just write something like pants and pullover should be fine.
‫And then finally, if it's higher than 20 degrees of it's warmer then.
‫Right.
‫Something like shorts are enough today.
‫All right, so now let's check it out.
‫Let's see what our program has to tell us.
‫And I'm going to enter, let's say, 25.
‫And it says Shorts are enough today.
‫Maybe we should enter a little text here which tells us what to do.
‫So console, dot, right line, what's the temperature like?
‫And I'll start it again.
‫What's the temperature like?
‫I'm going to say, all right.
‫Well, outside it's eight degrees.
‫Take the code.
‫All right.
‫I'm going to take my code.
‫It's fine.
‫Thank you very much.
‫So now we have a little program which is going to help us to decide what to wear based on the temperature.
‫Isn't that great?
‫So we have our first little program which actually has used or which is useful.
‫Yeah, of course you can go ahead and extend that a little bit.
‫And while that does work, this if if if approach here it's not the cleanest and we will see how we
‫can clean that up in the next video, how to make it even better and more effective, because currently
‫it's running this code, then it's running that code and then is running that code.
‫But the better approach would be to check if that's true.
‫Well, then the other ones can't be true.
‫So don't even bother running that code.
‫I told you about this approach not being super efficient, and I'm going to show you how to make it
‫more efficient, because what we have seen is if else, if and finally else.
‫All right.
‫So you have seen that in the first video of this chapter.
‫And now let's change the code that we have here to fit that structure.
‫You can try it yourself so you can pause the video and try to fit that as well.
‫Otherwise, just take a look at how I do it.
‫All right.
‫I hope you tried it.
‫So we check if the temperature is below 20, and then if that is true, then that will never be true.
‫So we won't need that code in case that is true, because it will just run and it's just unnecessary
‫processing power that we need.
‫But if we use ls instead so else if for example, then this code will only be run if that code here
‫is not true, or if that condition here is not true.
‫So now, if the temperature is not 20 or below 20, then we run that code.
‫If it's not below 20, then we go to the next line and we check is the temperature exactly 20.
‫And then we could go ahead and say something like else if and check for the next line.
‫So if temperature is higher than 20, then do that.
‫But that again would not be the most efficient way while it would work.
‫A more efficient way is simply to just use less here, because if that condition is not met and that
‫condition is not met, then that condition will run or the LL statement will run.
‫Because in our case it's pretty simple.
‫There are only those three options.
‫It's either below 20, exactly 20 or higher than 20.
‫Of course, we could other we could add other conditions like else.
‫If num temp is equal to 30, we could add that and that would be fine as well.
‫Then we will still have the condition of ls.
‫That means that all the other code.
‫So 21 to 29 would not be covered.
‫They would be covered by this ls and everything higher than 31 would be covered by this.
‫LS So we could approach that way or approach it that way, and then we could also have this higher than
‫30.
‫So if the temperature is higher than 30, then this code will be run, which would be, for example,
‫it's hot or it's super hot.
‫And all the other situations are so between 21 and 30 with this L statement will be run.
‫All right.
‫So these are the different options that we have, and this is a more efficient way than the one that
‫we have just seen.