1 00:00:00,250 --> 00:00:04,560 Let's take a second look at the if else statement we wrote in the previous video. 2 00:00:04,560 --> 00:00:07,180 This is a very basic if else statement that 3 00:00:07,180 --> 00:00:11,960 we can actually turn into a single line using the ternary if operator. 4 00:00:11,960 --> 00:00:15,480 The ternary if operator is a bit of syntactic sugar 5 00:00:15,480 --> 00:00:19,370 that can greatly simplify if else blocks like this. 6 00:00:19,370 --> 00:00:21,450 I'll show you how it's used here. 7 00:00:21,450 --> 00:00:26,212 The way we use this is to replace the if and else along with their curly braces 8 00:00:26,212 --> 00:00:29,622 with the question mark and colon characters like so. 9 00:00:46,610 --> 00:00:50,755 This is doing exactly the same thing that the previous code did. 10 00:00:50,755 --> 00:00:55,251 This will return a map location if path step is less than 11 00:00:55,251 --> 00:00:59,080 path.length otherwise it will return null. 12 00:01:00,550 --> 00:01:03,760 The return keyword is not part of the ternary if. 13 00:01:03,760 --> 00:01:09,040 We could have assigned the result of this expression to a variable instead. 14 00:01:10,680 --> 00:01:13,550 Returning two different values based on a condition, 15 00:01:13,550 --> 00:01:15,860 is a very common coding pattern. 16 00:01:15,860 --> 00:01:19,250 It's a good place to use the ternary if operator. 17 00:01:19,250 --> 00:01:23,360 In fact, you might see lots of places you could use the ternary if operator 18 00:01:23,360 --> 00:01:25,560 instead of using if else. 19 00:01:25,560 --> 00:01:27,640 You need to use your judgment though. 20 00:01:27,640 --> 00:01:32,620 Sometimes using this syntactic sugar in places where it isn't obvious 21 00:01:32,620 --> 00:01:36,690 why you're using it, can make code more difficult to understand. 22 00:01:36,690 --> 00:01:40,750 So only use it in cases where it's very obvious why it's happening. 23 00:01:41,870 --> 00:01:44,280 Part of the reason I'm showing it here is so 24 00:01:44,280 --> 00:01:47,840 that you can recognize it when you see it in code you might read in the future. 25 00:01:49,060 --> 00:01:53,190 Another good tip for using it is to use parenthesis. 26 00:01:53,190 --> 00:01:56,970 We don't actually need to use parentheses here, but 27 00:01:56,970 --> 00:02:00,550 i think it makes it more obvious what the condition is. 28 00:02:00,550 --> 00:02:04,046 You could also put parenthesis around these other operands to clarify 29 00:02:04,046 --> 00:02:05,476 the two different results. 30 00:02:08,101 --> 00:02:11,270 Let's compile this just to make sure we haven't broken anything. 31 00:02:11,270 --> 00:02:14,208 So going to the console ncs 32 00:02:14,208 --> 00:02:22,600 -out:Treehouse *.cs. 33 00:02:22,600 --> 00:02:24,260 Looks like we do have a compiler error. 34 00:02:24,260 --> 00:02:28,540 It says class struct or interface method must have a return type. 35 00:02:28,540 --> 00:02:29,879 Let's see here. 36 00:02:29,879 --> 00:02:33,369 Path.cs, line 12. 37 00:02:35,863 --> 00:02:39,939 Looks like I forgot to say that we want to return a MapLocation here. 38 00:02:43,692 --> 00:02:45,220 Let's try this again. 39 00:02:46,720 --> 00:02:48,070 There we go, it all works. 40 00:02:49,350 --> 00:02:51,408 It's a good thing we have a compiler to help us out?