‫Welcome back.
‫In this video, I will show you a shortcut for if statements and they are well enhanced if statements.
‫And what they can do is they can pretty much be a whole if statement it just in one row that's pretty
‫cool on one line and yeah so generally what you have is a condition then you have the question mark,
‫the first expression, colon, second expression, and then of course you end the statement with a semicolon.
‫So that's generally how you can create a shortcut of an if statement or an enhanced if statement.
‫So some points to consider here are the condition has to be either true or false.
‫The conditional operator is right.
‫So it's associative, which means that if we have a look at this expression here, well, actually,
‫let's have a look at that expression once we created our first little if statement.
‫So let's first create some variables here.
‫Temperature is going to be, let's say, minus five and we have a string and I'm calling that state
‫of matter.
‫So I'm going to leave it empty for now.
‫And if let's say the temperature is below zero, then I want the state of matter to be solid.
‫So state of matter is going to be solid.
‫All right.
‫As simple as that.
‫And else I want it to be state of matter is going to be liquid.
‫So temperature is zero.
‫But this I mean, Celsius.
‫So in Celsius and a zero degree water freezes.
‫That's why it was set to zero degree.
‫So that's why we start with a zero.
‫That's when water starts to freeze or it starts to become liquid.
‫So that's pretty much the turning point.
‫All right.
‫So the next thing that we want to do is simply write that on to the console.
‫So CW State of Matter is and then I'm going to position that into those curly brackets as the state
‫of matter.
‫So now if we check that, we also need to console read key here so that we see what's actually true
‫and let's run this code and then we are the state of matter is solid, so that means the temperature
‫is below zero.
‫Okay, that's the general statement that we had.
‫Of course, you could sum it up and you can make it even shorter.
‫So let's say we have a situation like this, you can do it like that.
‫So then you have two lines of code and that's very short, but that's usually not how you would create
‫an if else statement.
‫And the better way, of course, is to warn the better, but the shorter way is to use a enhanced if
‫statement.
‫So let's go ahead and do just that.
‫So I'm just going to create the whole thing in short, which means we have the state of matter is going
‫to be.
‫Temperature
‫below zero.
‫If that's true, then do the first.
‫If it's false to the second.
‫That's it.
‫So the whole thing that we had here in four lines, we could simply do in just one line.
‫So that's an enhanced if statement.
‫Now, if we get rid of all of that up to here and now, we run the code again.
‫So I'm just going to run it again.
‫And we see state of matter is solid.
‫And actually, let me uncomment that and simply increase the temperature from that point to the other
‫by 30 degrees.
‫So now it's going to be warm, 25 degrees and water is liquid at that point.
‫So now we have our old if statement and then we have the short one.
‫And now if we run that, we once have solid and once we have liquid.
‫All right.
‫So that's generally how you can go ahead and create a very simple version of.
‫An enhanced if statement now for you is a little challenge.
‫So the challenge is add the gas state of matter to the options.
‫So now use again an enhanced if statement to add well pretty much the gas state to it.
‫So one temperature is higher than 100 degrees.
‫All right.
‫I hope you tried it.
‫I hope you tried it in a similar way that we have seen here.
‫I haven't touched it yet, but I will do that in a second.
‫So let's go ahead and create the following.
‫We check the temperature once again and now we say, okay, is it higher than 100?
‫If that's true, then it's gas.
‫But if it's not true, then we want to check the temperature once again and we want to see if it's below
‫zero, because if it is below zero, then it's solid.
‫And if it's not below zero, then it's liquid.
‫So now we have a whole else, if and so forth, statement in one line.
‫So now if I run that and I increase the temperature by 100 degrees, temperature plus equal 100.
‫So now it's going to be 130.
‫And it should evaporate.
‫It should become gas.
‫So now we should get the information that it is gas.
‫State of matter, solid.
‫That was the first one.
‫And now state of matter gas.
‫That's the one that we have here.
‫All right.
‫So what is important here?
‫What we have is we check.
‫If the temperature is higher than 100.
‫If that is true, then we run this code here, or we simply use gas as the state of matter.
‫But if it's not, then we have a second check.
‫So we check.
‫What is going on here?
‫Is the temperature below zero?
‫And if it is, yes.
‫If it's true, then the first entry is true.
‫Otherwise, the second one.
‫So we don't check it like this here.
‫We don't say, okay.
‫We check all of that.
‫If that is true, that whole statement, then it's solid.
‫Otherwise, it's liquid.
‫That's not what we're doing.
‫It's really important that you understand that what we are doing is we check.
‫The first.
‫Actually, we can do it like that.
‫We could, of course, see it in a way that we have this statement that we check.
‫But first we have this statement here, so we check.
‫Is this condition true?
‫Then we run that.
‫Otherwise we run this whole thing here.
‫What is inside of the brackets?
‫But that's not what you need to write.
‫You can simply leave it like that.
‫And that's what this statement here is saying.
‫So the expression A, B, C, D, E, that's what we have at the bottom is evaluated as is a true.
‫If yes, then B otherwise check is C true, then D otherwise e.
‫So that's pretty much what we have done.
‫And it's important that it's not like you see here.
‫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
‫it.
‫And finally, the conditional operator cannot be overloaded.
‫All right.
‫So that's generally the enhanced F statement.
‫I hope you could manage to solve this challenge, and it will come in handy whenever you want to use
‫very simple and short sentences.
‫By the way, if we would now change our, if else, statement to what we have here at the bottom, it
‫would look like this else if temperature is higher than 100.
‫And finally else we have the state of matter is going to be.
‫Here.
‫It's going to be liquid, by the way, and this one here would be gas.
‫So it's not exactly the same approach.
‫I just want to show you that we need six lines here and that's even without using the curly brackets.
‫Or we just have one line where we handle all of the same code.