1 00:00:00,150 --> 00:00:07,080 ‫Now that we know how to use if statements, we can also make sure that the code runs flawlessly. 2 00:00:07,410 --> 00:00:15,420 ‫So when the user, for example, enters an incorrect value here like a letter, then our code crashes 3 00:00:15,420 --> 00:00:17,750 ‫and our program doesn't run anymore. 4 00:00:17,760 --> 00:00:23,920 ‫So in order to fix that, we can simply use try parse instead of parse. 5 00:00:23,940 --> 00:00:30,150 ‫So what I'm going to do is I'm going to use int, try parse and I'm trying to parse the temperature 6 00:00:30,720 --> 00:00:37,860 ‫and I need to give an output here and I'm going to use a new integer, which I'm going to call number 7 00:00:39,090 --> 00:00:41,370 ‫and use that number. 8 00:00:42,720 --> 00:00:49,080 ‫So now if it works out, I want num temp to be number. 9 00:00:50,640 --> 00:00:57,360 ‫So now I won't need to set it here anymore with the parse statement because I have replaced it with 10 00:00:57,360 --> 00:00:58,950 ‫a safe approach. 11 00:01:00,150 --> 00:01:05,620 ‫Now here it's complaining because it could be empty, so num temp could be empty. 12 00:01:05,640 --> 00:01:12,780 ‫So what I'm going to do is if the user didn't enter the correct value, I'm just going to assume that 13 00:01:13,590 --> 00:01:20,790 ‫he didn't want to run the calculation correctly and I'm just going to set num temp to being zero. 14 00:01:22,530 --> 00:01:29,160 ‫So now in both cases, so if it worked to parse or if it didn't work to parse, num temp is going to 15 00:01:29,160 --> 00:01:30,090 ‫have a value. 16 00:01:30,120 --> 00:01:33,630 ‫If it worked, then it's just going to be the value that the user has entered. 17 00:01:33,660 --> 00:01:36,300 ‫If it didn't work, then it's just going to be zero. 18 00:01:36,840 --> 00:01:40,260 ‫And now we can go ahead and just run our code. 19 00:01:40,260 --> 00:01:43,950 ‫But at the same time, if the user didn't enter a correct value. 20 00:01:43,980 --> 00:01:49,290 ‫What I want to do, I want to tell him that he didn't enter the correct value. 21 00:01:49,500 --> 00:01:51,690 ‫So I'm going to say. 22 00:01:53,490 --> 00:01:57,300 ‫Value entered was no number 23 00:02:00,540 --> 00:02:05,760 ‫zero said as temperature because that's exactly what is true. 24 00:02:05,760 --> 00:02:06,300 ‫Right? 25 00:02:06,480 --> 00:02:14,460 ‫So let's run the code and now let's see what happens if I enter something that's not just the number 26 00:02:15,270 --> 00:02:18,870 ‫it says value entered was no number zero set is temperature. 27 00:02:18,870 --> 00:02:27,540 ‫And it's saying take a code because with zero, that condition here is true and we simply get to take 28 00:02:27,540 --> 00:02:28,230 ‫the code. 29 00:02:29,280 --> 00:02:29,790 ‫All right. 30 00:02:29,790 --> 00:02:36,030 ‫So I hope that wasn't too overwhelming with this trip pass, because so far we have used parse and parse 31 00:02:36,030 --> 00:02:37,110 ‫was super simple. 32 00:02:37,110 --> 00:02:42,690 ‫We just said int that parse and then we entered the value that we want to parse. 33 00:02:42,690 --> 00:02:51,960 ‫So the string in this case the temperature and that would return an integer like int my num it would 34 00:02:51,960 --> 00:02:53,250 ‫just return an integer. 35 00:02:53,250 --> 00:02:59,280 ‫But the problem is if the user doesn't enter the correct value like a number in general, then this 36 00:02:59,310 --> 00:03:00,480 ‫leads to crash. 37 00:03:00,480 --> 00:03:05,790 ‫So in order to avoid that, we use try parse. 38 00:03:05,790 --> 00:03:13,680 ‫So what we do here is we use in try parse, then we say what we want to parse and it's the temperature 39 00:03:13,680 --> 00:03:21,540 ‫and then we tell where to use the or what to put the number that is the result of parsing if it worked. 40 00:03:21,660 --> 00:03:24,210 ‫So what we check here is did it work? 41 00:03:24,210 --> 00:03:28,080 ‫Because the result of this statement here is a boolean. 42 00:03:28,260 --> 00:03:44,970 ‫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 43 00:03:44,970 --> 00:03:51,810 ‫then I just run that code so now I can just check if user entered a number is true. 44 00:03:51,810 --> 00:03:58,860 ‫So if that's true, then just use the number that was output and assign that to num temp. 45 00:03:58,890 --> 00:04:05,700 ‫And if it didn't work, then just enter zero and inform the user that he didn't enter the right value. 46 00:04:05,700 --> 00:04:06,150 ‫All right. 47 00:04:06,150 --> 00:04:09,870 ‫So as I said, the result of that statement here is a boolean. 48 00:04:09,870 --> 00:04:13,350 ‫So we can simply store it in a Boolean and check that boolean. 49 00:04:13,350 --> 00:04:17,490 ‫But we could, of course, also simply take that code and run it in here. 50 00:04:19,050 --> 00:04:19,680 ‫Great. 51 00:04:19,680 --> 00:04:27,540 ‫So now you've seen how to use try parse how to use if statements ls else, if and so forth. 52 00:04:27,540 --> 00:04:32,820 ‫We're going to have a look at more complex and nested if statements in the next video. 53 00:04:32,820 --> 00:04:33,930 ‫So see you there.