‫Now that we know how to use if statements, we can also make sure that the code runs flawlessly.
‫So when the user, for example, enters an incorrect value here like a letter, then our code crashes
‫and our program doesn't run anymore.
‫So in order to fix that, we can simply use try parse instead of parse.
‫So what I'm going to do is I'm going to use int, try parse and I'm trying to parse the temperature
‫and I need to give an output here and I'm going to use a new integer, which I'm going to call number
‫and use that number.
‫So now if it works out, I want num temp to be number.
‫So now I won't need to set it here anymore with the parse statement because I have replaced it with
‫a safe approach.
‫Now here it's complaining because it could be empty, so num temp could be empty.
‫So what I'm going to do is if the user didn't enter the correct value, I'm just going to assume that
‫he didn't want to run the calculation correctly and I'm just going to set num temp to being zero.
‫So now in both cases, so if it worked to parse or if it didn't work to parse, num temp is going to
‫have a value.
‫If it worked, then it's just going to be the value that the user has entered.
‫If it didn't work, then it's just going to be zero.
‫And now we can go ahead and just run our code.
‫But at the same time, if the user didn't enter a correct value.
‫What I want to do, I want to tell him that he didn't enter the correct value.
‫So I'm going to say.
‫Value entered was no number
‫zero said as temperature because that's exactly what is true.
‫Right?
‫So let's run the code and now let's see what happens if I enter something that's not just the number
‫it says value entered was no number zero set is temperature.
‫And it's saying take a code because with zero, that condition here is true and we simply get to take
‫the code.
‫All right.
‫So I hope that wasn't too overwhelming with this trip pass, because so far we have used parse and parse
‫was super simple.
‫We just said int that parse and then we entered the value that we want to parse.
‫So the string in this case the temperature and that would return an integer like int my num it would
‫just return an integer.
‫But the problem is if the user doesn't enter the correct value like a number in general, then this
‫leads to crash.
‫So in order to avoid that, we use try parse.
‫So what we do here is we use in try parse, then we say what we want to parse and it's the temperature
‫and then we tell where to use the or what to put the number that is the result of parsing if it worked.
‫So what we check here is did it work?
‫Because the result of this statement here is a boolean.
‫So if I just copy that out and I create a new bool bul did work or let's say user entered a number and
‫then I just run that code so now I can just check if user entered a number is true.
‫So if that's true, then just use the number that was output and assign that to num temp.
‫And if it didn't work, then just enter zero and inform the user that he didn't enter the right value.
‫All right.
‫So as I said, the result of that statement here is a boolean.
‫So we can simply store it in a Boolean and check that boolean.
‫But we could, of course, also simply take that code and run it in here.
‫Great.
‫So now you've seen how to use try parse how to use if statements ls else, if and so forth.
‫We're going to have a look at more complex and nested if statements in the next video.
‫So see you there.