1 00:00:00,090 --> 00:00:00,780 ‫Welcome back. 2 00:00:00,810 --> 00:00:05,340 ‫In this video, I would like to show you my solution to the ternary operator challenge. 3 00:00:05,340 --> 00:00:08,000 ‫And of course, use could look differently. 4 00:00:08,010 --> 00:00:10,640 ‫So I'd say let's just get started. 5 00:00:10,650 --> 00:00:17,640 ‫So the first thing that we will need is a input temperature, then the temperature message as well as 6 00:00:17,640 --> 00:00:18,510 ‫the input value. 7 00:00:18,510 --> 00:00:20,400 ‫And I'm using string empty here. 8 00:00:20,400 --> 00:00:26,310 ‫You could of course just let it be an empty string, but it really opens up other opportunities to work 9 00:00:26,310 --> 00:00:28,080 ‫with the string when we do it like this. 10 00:00:28,260 --> 00:00:35,700 ‫So what we first of all need to know is the temperature that the user enters and we get the input from 11 00:00:35,700 --> 00:00:39,420 ‫the console by using the console that read line. 12 00:00:40,140 --> 00:00:49,470 ‫And then we want to validate if the value that the user entered is in fact a valid integer or not. 13 00:00:49,470 --> 00:00:52,590 ‫And here we use try parse and we've done that multiple times. 14 00:00:52,590 --> 00:00:54,480 ‫So I'm just going to keep it simple here. 15 00:00:54,480 --> 00:00:57,750 ‫So just use this boolean to know if it was valid or not. 16 00:00:57,750 --> 00:01:03,360 ‫And based on that, we can then go ahead and do the actual paternity operator. 17 00:01:03,420 --> 00:01:11,070 ‫So I'm going to use an if statement with an LS statement here to check for the valid integer, but for 18 00:01:11,100 --> 00:01:15,780 ‫the actual temperature, I'm going to use a ternary operator. 19 00:01:16,200 --> 00:01:17,760 ‫So let's look at this. 20 00:01:17,760 --> 00:01:21,720 ‫So what I have here is this if statement if valid integer. 21 00:01:21,720 --> 00:01:27,780 ‫So if the input that the user entered is in fact an integer, then this code plug will be executed. 22 00:01:27,780 --> 00:01:30,300 ‫And what is going to happen here? 23 00:01:30,570 --> 00:01:33,740 ‫And by the way, of course, it's message with one eight. 24 00:01:33,750 --> 00:01:42,300 ‫So what I have here is I say input temperature less or equal 15 So this is my statement because here 25 00:01:42,300 --> 00:01:48,540 ‫I'm using the ternary operator and you can see it's a multi level ternary operator which can become 26 00:01:48,540 --> 00:01:49,260 ‫quite complex. 27 00:01:49,260 --> 00:01:51,570 ‫So I'm going to go through it step by step. 28 00:01:51,570 --> 00:01:54,540 ‫So this here is basically my condition. 29 00:01:54,540 --> 00:01:59,450 ‫So I'm checking, checking is input temperature less or equal? 30 00:01:59,460 --> 00:02:01,590 ‫15 Now I'm checking. 31 00:02:01,590 --> 00:02:05,580 ‫If that is true then state it is too cold here. 32 00:02:05,880 --> 00:02:07,080 ‫So this is. 33 00:02:08,580 --> 00:02:10,410 ‫The true case. 34 00:02:11,030 --> 00:02:14,270 ‫And then what happens in the false case? 35 00:02:16,410 --> 00:02:19,490 ‫In the first case, what I have is another condition. 36 00:02:19,500 --> 00:02:21,090 ‫So here I'm checking again. 37 00:02:21,480 --> 00:02:28,380 ‫Is the input temperature greater equals 16 and less equal 28. 38 00:02:28,380 --> 00:02:31,470 ‫So is it between 16 and to 28 degrees? 39 00:02:31,470 --> 00:02:33,660 ‫And we're talking about degrees Celsius here. 40 00:02:34,110 --> 00:02:35,850 ‫And then I have another check. 41 00:02:35,850 --> 00:02:39,960 ‫So I'm saying if it is true, execute this here. 42 00:02:40,590 --> 00:02:43,500 ‫So just say it's cold here. 43 00:02:43,650 --> 00:02:48,060 ‫Otherwise the this year I'll run the next code. 44 00:02:48,060 --> 00:02:51,240 ‫So in our case we have another check. 45 00:02:51,240 --> 00:02:53,760 ‫So here, by the way, this is for the case. 46 00:02:53,760 --> 00:02:54,450 ‫It's true. 47 00:02:54,990 --> 00:02:56,490 ‫And this is for the case. 48 00:02:56,490 --> 00:02:57,720 ‫It's false. 49 00:02:59,120 --> 00:03:02,600 ‫And then we have this check in temperature crater 28. 50 00:03:02,840 --> 00:03:09,580 ‫If that is the case, print, it is hot here and else an empty string. 51 00:03:09,590 --> 00:03:14,000 ‫So because we have all of those other checks here already. 52 00:03:14,210 --> 00:03:21,080 ‫So if it's less than 15 and if it's higher than 28 and we have all of the cases in between, then basically 53 00:03:21,080 --> 00:03:25,130 ‫this case will never be happening or it will never actually happen. 54 00:03:25,130 --> 00:03:28,610 ‫But we still need to maintain the structure. 55 00:03:29,120 --> 00:03:30,590 ‫So this is the condition. 56 00:03:30,680 --> 00:03:33,710 ‫Then we say it's a ternary operator. 57 00:03:34,010 --> 00:03:38,180 ‫Then we say, This is what's going to happen if it's true and this is what's going to happen if it's 58 00:03:38,180 --> 00:03:38,390 ‫false. 59 00:03:38,400 --> 00:03:40,460 ‫So after the colon each time. 60 00:03:41,270 --> 00:03:41,930 ‫So I can. 61 00:03:41,930 --> 00:03:42,620 ‫Of course. 62 00:03:43,800 --> 00:03:45,660 ‫Have the same structure here again. 63 00:03:45,660 --> 00:03:46,830 ‫So I'm saying. 64 00:03:47,040 --> 00:03:47,850 ‫True. 65 00:03:48,330 --> 00:03:49,530 ‫And I'm saying. 66 00:03:50,770 --> 00:03:51,460 ‫False. 67 00:03:52,890 --> 00:03:53,550 ‫All right. 68 00:03:53,730 --> 00:03:59,670 ‫So this is our pretty long ternary operator with multiple cases in there. 69 00:03:59,670 --> 00:04:02,480 ‫But overall, that's how you can use it. 70 00:04:02,940 --> 00:04:08,160 ‫So the general structure is always you have the condition. 71 00:04:10,090 --> 00:04:11,470 ‫Condition. 72 00:04:12,310 --> 00:04:14,200 ‫Then question mark. 73 00:04:15,090 --> 00:04:16,020 ‫If true. 74 00:04:16,530 --> 00:04:18,840 ‫Colon if false. 75 00:04:19,870 --> 00:04:21,540 ‫So what if the condition is true? 76 00:04:21,550 --> 00:04:22,890 ‫This will be executed. 77 00:04:22,900 --> 00:04:25,270 ‫If the condition is false, this will be executed. 78 00:04:26,410 --> 00:04:26,920 ‫Okay. 79 00:04:27,070 --> 00:04:33,580 ‫So then in the case we just say not a valid temperature and now let's just run this code to see if it's 80 00:04:33,580 --> 00:04:37,150 ‫actually going to work or if I had a typo there or something like that. 81 00:04:37,270 --> 00:04:40,140 ‫So enter the current temperature. 82 00:04:40,150 --> 00:04:41,680 ‫Let me see in my room. 83 00:04:41,680 --> 00:04:42,910 ‫It's quite hot. 84 00:04:42,910 --> 00:04:48,220 ‫So I'm just going to say it's 25 degrees and it says it's cold here. 85 00:04:48,610 --> 00:04:52,150 ‫That's because that's what the statement is saying here. 86 00:04:52,150 --> 00:04:56,260 ‫So this really depends on the feeling that you have. 87 00:04:56,260 --> 00:05:01,630 ‫So if you are coming from a super hot country, then you would say 28 degrees is still considered cold. 88 00:05:01,630 --> 00:05:02,140 ‫I don't know. 89 00:05:02,140 --> 00:05:07,570 ‫But if you are from the north of Europe, for example, or from Canada, then you would say, well, 90 00:05:07,600 --> 00:05:11,080 ‫28 degrees Celsius, man, this is the hottest summer ever. 91 00:05:11,290 --> 00:05:13,210 ‫So it's always relative. 92 00:05:13,210 --> 00:05:15,610 ‫So you can of course, play around with those values. 93 00:05:16,120 --> 00:05:19,660 ‫Okay, so that's it for ternary operators. 94 00:05:19,660 --> 00:05:24,760 ‫I hope you manage to get this done correctly and see you in the next video.