1 00:00:04,552 --> 00:00:08,939 I was showing off our app to a coworker of mine and she said wouldn't it be great if 2 00:00:08,939 --> 00:00:13,940 the user saw some positive feedback when they entered their exercise time? 3 00:00:13,940 --> 00:00:15,220 I thought that was a great idea. 4 00:00:16,325 --> 00:00:19,030 Oftentimes we get near the end of a project and 5 00:00:19,030 --> 00:00:22,050 someone like the client requests a new feature. 6 00:00:22,050 --> 00:00:25,870 This is actually very common in professional software development. 7 00:00:25,870 --> 00:00:29,570 Lucky for us adding this new feature won't take much. 8 00:00:29,570 --> 00:00:33,310 We don't even need to rewrite any of the code we've already written. 9 00:00:33,310 --> 00:00:35,170 So how do we want this to work? 10 00:00:35,170 --> 00:00:39,200 I say we print out a message each time they add a new entry. 11 00:00:39,200 --> 00:00:43,040 The message can be different depending on how much the exercise they did. 12 00:00:43,040 --> 00:00:46,790 If they did 10 or less minutes of exercise we could say something like 13 00:00:46,790 --> 00:00:48,820 better than nothing, am I right? 14 00:00:48,820 --> 00:00:53,580 If they did between 10 and 30 minutes we could say way to go hot stuff. 15 00:00:53,580 --> 00:00:54,760 If they did between 30 and 16 00:00:54,760 --> 00:00:59,400 60 minutes we could say you must be a ninja warrior in training. 17 00:00:59,400 --> 00:01:02,810 And if they did more than 60 minutes we could say okay, 18 00:01:02,810 --> 00:01:03,770 now you're just showing off. 19 00:01:04,980 --> 00:01:07,950 Nice. Let's open workspaces and code this up. 20 00:01:09,190 --> 00:01:13,430 The first thing we need to do is figure out where we want to insert this code. 21 00:01:13,430 --> 00:01:16,110 We know we need to look at the value of this minutes variable. 22 00:01:16,110 --> 00:01:18,880 So it will need to be somewhere after here. 23 00:01:18,880 --> 00:01:24,270 And we probably want our message to print out before we print out the running total. 24 00:01:24,270 --> 00:01:25,710 I think here is a good place to put it. 25 00:01:26,740 --> 00:01:29,560 We already know that we can compare two strings to see if they're 26 00:01:29,560 --> 00:01:30,850 equal to one another. 27 00:01:30,850 --> 00:01:33,570 It turns out we can also compare integers. 28 00:01:33,570 --> 00:01:37,630 In fact, we can even compare if an integer is greater than or 29 00:01:37,630 --> 00:01:39,830 less than another integer. 30 00:01:39,830 --> 00:01:41,910 Let's check this out in the repel. 31 00:01:41,910 --> 00:01:46,480 We can use the comparison operators for equals and not equals on integers. 32 00:01:46,480 --> 00:01:50,540 We can also compare to see if one integer is greater than the other. 33 00:01:50,540 --> 00:01:51,850 What do you think this will give us? 34 00:01:53,000 --> 00:01:53,880 You guessed it. 35 00:01:53,880 --> 00:01:57,480 The less than operator returns true if the value on the left 36 00:01:57,480 --> 00:01:59,730 is less than the value on the right. 37 00:01:59,730 --> 00:02:02,970 We can reverse it by using the greater than operator instead. 38 00:02:02,970 --> 00:02:04,700 Now what will we get? 39 00:02:04,700 --> 00:02:06,510 This time we get false. 40 00:02:06,510 --> 00:02:08,770 There's also a less than or equal to. 41 00:02:08,770 --> 00:02:12,040 5 is less than or equal to 6. 42 00:02:12,040 --> 00:02:14,160 It's also less than or equal to 5. 43 00:02:14,160 --> 00:02:17,640 The same with greater than or equal to. 44 00:02:17,640 --> 00:02:22,430 The greater than and less than comparisons work on numeric types, like int. 45 00:02:22,430 --> 00:02:25,340 But they don't work on strings in C sharp. 46 00:02:25,340 --> 00:02:26,730 Let's go back to our code and 47 00:02:26,730 --> 00:02:31,320 use what we just learned about comparison operators to implement our new feature. 48 00:02:31,320 --> 00:02:33,610 First, we're going to need an if statement. 49 00:02:34,960 --> 00:02:39,040 Here in the condition, we'll check to see if minutes is less than or equal to 10. 50 00:02:39,040 --> 00:02:43,211 Here in the body, we'll print out our positive feedback. 51 00:02:46,451 --> 00:02:50,366 Then we can make another if statement to check if they exercise between 10 and 52 00:02:50,366 --> 00:02:51,710 30 minutes. 53 00:02:51,710 --> 00:02:55,490 For that we need to introduce another clause to our if statement. 54 00:02:55,490 --> 00:02:56,270 I'll write it out here. 55 00:02:57,610 --> 00:02:59,370 We've already learned about else. 56 00:02:59,370 --> 00:03:03,980 The body of the else gets run only if the condition in the if was false. 57 00:03:03,980 --> 00:03:06,510 But we often want to check multiple conditions and 58 00:03:06,510 --> 00:03:09,070 only do something if one of them is true. 59 00:03:09,070 --> 00:03:13,180 That's the purpose of the else if clause, and it works perfectly for what we need. 60 00:03:13,180 --> 00:03:17,180 So in our if, we checked if minutes is less than or equal to 10. 61 00:03:17,180 --> 00:03:22,790 If it isn't, the program will continue and check the condition in the else if. 62 00:03:22,790 --> 00:03:26,820 In fact, we can add as many else if clauses as we need. 63 00:03:26,820 --> 00:03:31,380 So here, we want to check if minutes is less than or equal to 30 and 64 00:03:31,380 --> 00:03:32,917 print our message if that's true. 65 00:03:35,510 --> 00:03:40,210 Now the program will only print this out if minutes is greater than 10 or 66 00:03:40,210 --> 00:03:42,610 less than or equal to 30. 67 00:03:42,610 --> 00:03:46,990 The reason this will only run for minutes greater than 10 is because if minutes was 68 00:03:46,990 --> 00:03:52,420 less than or equal to 10 the program would have run this code block instead. 69 00:03:52,420 --> 00:03:56,140 Remember only one of these code blocks will be run. 70 00:03:56,140 --> 00:04:00,667 Continuing on with that logic, we can fill in the next else if statement. 71 00:04:07,560 --> 00:04:12,460 Now the else portion only gets run if none of these others were run. 72 00:04:12,460 --> 00:04:16,714 These cover any number of minutes less than or equal to 60. 73 00:04:16,714 --> 00:04:22,820 So if the user enters anything over 60, the program will enter the else clause. 74 00:04:22,820 --> 00:04:24,417 This is where we want to put that code. 75 00:04:29,568 --> 00:04:31,780 Let's compile this and check it out. 76 00:04:31,780 --> 00:04:39,362 Put the REPL, than clear, compile, and run. 77 00:04:41,280 --> 00:04:45,300 If we enter 1, we get better than nothing, am I right? 78 00:04:45,300 --> 00:04:48,680 If we enter 30, we get way to go hot stuff. 79 00:04:48,680 --> 00:04:53,730 If we enter 31, we get you must be a ninja warrior in training. 80 00:04:53,730 --> 00:04:57,330 And if we enter 90, we get okay, now you're just showing off. 81 00:04:58,480 --> 00:05:00,010 Everything looks like it still works. 82 00:05:00,010 --> 00:05:01,310 And we can type quit to exit.