1 00:00:00,150 --> 00:00:03,840 Before we start, did you try solving the workbook yourself? 2 00:00:03,870 --> 00:00:09,630 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,760 --> 00:00:13,580 This video will present the solution to workbook 3.6. 4 00:00:13,580 --> 00:00:18,830 Please make sure that you launch the exact folder that contains your java file, otherwise you will 5 00:00:18,830 --> 00:00:21,290 not be able to visualize the runtime with me. 6 00:00:21,620 --> 00:00:23,780 All right, let's do this. 7 00:00:23,900 --> 00:00:25,520 Zooming in a little bit. 8 00:00:25,520 --> 00:00:32,630 So your friend wants to hang out on a Friday and we need to check that day against seven days from your 9 00:00:32,630 --> 00:00:33,380 calendar. 10 00:00:33,650 --> 00:00:37,330 The calendar, I hope you guessed it is just going to be a switch statement. 11 00:00:37,340 --> 00:00:39,230 So here's a trick for you. 12 00:00:39,260 --> 00:00:42,050 You can actually autocomplete a switch statement. 13 00:00:42,050 --> 00:00:46,820 So write switch and click on this option where it says switch statement. 14 00:00:48,140 --> 00:00:55,820 And here we're going to be comparing the day that your friend wants to hang out against a list of seven 15 00:00:55,820 --> 00:00:56,630 cases. 16 00:00:56,630 --> 00:00:58,610 So case Monday. 17 00:01:00,240 --> 00:01:04,980 If the day key matches a value of Monday, then we're going to print. 18 00:01:07,530 --> 00:01:10,320 System don't print line. 19 00:01:12,050 --> 00:01:12,950 Sorry. 20 00:01:12,950 --> 00:01:14,840 I have to stay at work late. 21 00:01:16,520 --> 00:01:21,410 And you'll notice that it automatically adds a break keyword at the end of our case, which is perfect 22 00:01:21,410 --> 00:01:27,320 because in the event of a case match, we're not going to want to execute the remaining cases. 23 00:01:27,320 --> 00:01:30,020 We want the entire switch statement to break. 24 00:01:30,050 --> 00:01:31,550 Let's copy and paste this. 25 00:01:31,550 --> 00:01:36,580 One, two, three, four, five, six more times. 26 00:01:36,590 --> 00:01:41,360 This is going to be Tuesday, in which case we're going to print the following. 27 00:01:43,910 --> 00:01:45,590 It looks like I have meetings all day. 28 00:01:46,340 --> 00:01:47,300 Wednesday. 29 00:01:49,050 --> 00:01:53,640 Feel free to pause the video and do the rest on your own and resume once you are done. 30 00:01:59,380 --> 00:01:59,770 All right. 31 00:01:59,770 --> 00:02:01,480 You probably got something like this. 32 00:02:01,480 --> 00:02:07,420 You'll notice that I removed the brake keyword that was automatically generated for the default case. 33 00:02:07,420 --> 00:02:11,550 And that's because it's logically unnecessary to have a brake keyword here. 34 00:02:11,560 --> 00:02:15,250 Let's just say they were to say, I don't know, laundry day here or something. 35 00:02:15,250 --> 00:02:21,700 That's not going to end up matching any of the cases eventually reaching the default case, which is 36 00:02:21,700 --> 00:02:24,010 the last possible case that will run. 37 00:02:24,010 --> 00:02:29,320 So putting brake here to break the switch statement after won't really do anything right. 38 00:02:29,320 --> 00:02:31,210 You can keep it, you can remove it. 39 00:02:31,210 --> 00:02:32,320 It doesn't really matter. 40 00:02:32,320 --> 00:02:37,750 But for tutorial purposes to prove to you that it's not necessary to have it, I'm going to remove it. 41 00:02:38,140 --> 00:02:39,040 All right. 42 00:02:39,040 --> 00:02:41,170 And now let's visualize the runtime. 43 00:02:41,170 --> 00:02:46,780 So just like you would visualize a series of if, if, and when you have a series of cases, you can 44 00:02:46,780 --> 00:02:52,750 just put a breakpoint right at the very beginning and use the familiar step over button. 45 00:02:53,170 --> 00:02:55,600 And now we're going to add breakpoints here as well. 46 00:02:55,630 --> 00:02:58,630 Let's start with the first test case, which was Friday. 47 00:02:59,290 --> 00:03:02,800 If your friend wants to hang out on a Friday. 48 00:03:07,240 --> 00:03:12,510 We'll continue to the next breakpoint day equals Friday, and now it's going to print. 49 00:03:12,520 --> 00:03:14,130 Are you free on that day? 50 00:03:14,140 --> 00:03:16,130 We'll continue to the next breakpoint. 51 00:03:16,150 --> 00:03:21,580 Do not press continue here because continue will try to continue to the next breakpoint. 52 00:03:21,580 --> 00:03:23,140 But there is no other breakpoint. 53 00:03:23,140 --> 00:03:29,140 So it's not going to allow us to visualize the runtime press step over so that we can step over to the 54 00:03:29,140 --> 00:03:31,930 matching case, which is Friday. 55 00:03:31,930 --> 00:03:32,860 I am free. 56 00:03:32,860 --> 00:03:36,480 And so upon pressing continue it prints I am free. 57 00:03:36,490 --> 00:03:39,040 Test case number two is to put Saturday. 58 00:03:40,730 --> 00:03:43,070 We shouldn't really see anything different. 59 00:03:44,670 --> 00:03:50,340 Again here, press step over, not continue, because there are no other break points to continue to. 60 00:03:50,370 --> 00:03:54,330 So we'll step over to the matching case, which is Saturday. 61 00:03:54,330 --> 00:04:00,270 And upon executing this line of code, it's going to print I am free and break is going to break the 62 00:04:00,270 --> 00:04:01,320 switch statement. 63 00:04:01,950 --> 00:04:02,790 Beautiful. 64 00:04:02,820 --> 00:04:04,950 Now, what if we put Monday? 65 00:04:06,520 --> 00:04:08,050 Debugging the runtime. 66 00:04:10,030 --> 00:04:10,840 Same thing. 67 00:04:10,840 --> 00:04:13,780 The first matching case is Monday printing. 68 00:04:13,780 --> 00:04:17,560 Sorry, I have to stay at work late and then breaking the switch statement. 69 00:04:18,730 --> 00:04:22,089 If you forget to put the break word, what would happen? 70 00:04:22,210 --> 00:04:24,130 So let's visualize the runtime. 71 00:04:27,600 --> 00:04:31,750 Clearly the matching case is Monday upon a case match. 72 00:04:31,770 --> 00:04:36,240 If you don't put brake, it's just going to run every other case that follows. 73 00:04:36,240 --> 00:04:37,880 That is not good. 74 00:04:37,890 --> 00:04:41,880 But now since we have brake over here, then it's going to break the switch statement. 75 00:04:41,880 --> 00:04:47,790 So your friend wanted to hang out on a monday, but we printed out answers for Monday and Tuesday. 76 00:04:47,820 --> 00:04:50,160 If you were to remove this brake keyword as well. 77 00:04:52,420 --> 00:04:53,590 Let's continue. 78 00:04:54,280 --> 00:04:56,740 Continue stepping over here. 79 00:04:56,740 --> 00:04:58,060 We have a case match. 80 00:04:58,060 --> 00:05:03,370 So the switch statement is going to run every other case that follows if we don't have a break keyword. 81 00:05:05,250 --> 00:05:07,140 It runs the case for Tuesday. 82 00:05:07,380 --> 00:05:10,050 And now it's also going to run the case for Wednesday. 83 00:05:10,050 --> 00:05:15,090 But now we have the brake keyword, so it's going to ultimately break. 84 00:05:17,020 --> 00:05:17,970 All right. 85 00:05:17,980 --> 00:05:19,600 What if we had Friday? 86 00:05:20,700 --> 00:05:25,320 And we were to remove the breaking word for Friday, Saturday and Sunday. 87 00:05:25,800 --> 00:05:26,910 What would happen then? 88 00:05:26,910 --> 00:05:28,530 Let's visualize the runtime. 89 00:05:30,490 --> 00:05:32,740 The matching case is going to be Friday. 90 00:05:32,740 --> 00:05:38,410 And after there is a case match, it's going to run every other case that follows, which is exactly 91 00:05:38,440 --> 00:05:39,750 what we don't want. 92 00:05:39,760 --> 00:05:40,690 It's going to print. 93 00:05:40,690 --> 00:05:42,520 I'm free for the case Friday. 94 00:05:42,550 --> 00:05:43,390 It's going to print. 95 00:05:43,390 --> 00:05:45,490 I'm free for the case of Saturday. 96 00:05:45,520 --> 00:05:46,300 It's going to print. 97 00:05:46,300 --> 00:05:48,070 I'm free for the case of Sunday. 98 00:05:48,100 --> 00:05:50,140 And it's also going to run the default case. 99 00:05:50,140 --> 00:05:51,040 That's not a date. 100 00:05:51,070 --> 00:05:57,650 So I hope you realize now how important it is to have the break keyword after each case. 101 00:05:57,670 --> 00:06:04,330 Now you're able to visualize the runtime in real time and understand exactly what everything is doing. 102 00:06:04,660 --> 00:06:08,230 So now if I put Friday restarting. 103 00:06:10,300 --> 00:06:13,870 Now, this time the matching case is Friday. 104 00:06:13,870 --> 00:06:16,720 So it's going to execute the code for this case. 105 00:06:16,720 --> 00:06:20,980 And switch is designed to run every other case that follows. 106 00:06:20,980 --> 00:06:25,780 But because we have the break keyword, it's going to break out of the switch statement. 107 00:06:25,780 --> 00:06:28,600 This is working exactly as we want it to. 108 00:06:28,630 --> 00:06:32,500 Again, the break keyword is really, really important to include. 109 00:06:32,500 --> 00:06:38,140 After every case, you don't have to put it for a default because it would be the last thing that runs 110 00:06:38,140 --> 00:06:38,830 anyway. 111 00:06:39,250 --> 00:06:42,070 All right, let's execute the default case. 112 00:06:42,070 --> 00:06:43,930 What if we put a laundry day? 113 00:06:43,930 --> 00:06:45,730 Let's visualize the runtime. 114 00:06:47,630 --> 00:06:48,080 All right. 115 00:06:48,080 --> 00:06:54,260 Do not press continue because there are no other breakpoints that continue to let us step over to the 116 00:06:54,260 --> 00:06:55,130 matching case. 117 00:06:55,160 --> 00:06:56,600 There were no matching cases. 118 00:06:56,600 --> 00:06:59,210 So the default case runs printing. 119 00:06:59,210 --> 00:07:03,920 That's not a day Now we can press continue because we are done. 120 00:07:05,250 --> 00:07:05,730 All right. 121 00:07:05,730 --> 00:07:11,520 I really hope you enjoyed this breakpoint session, and I hope visualizing the runtime in real time 122 00:07:11,520 --> 00:07:15,780 was able to shed some light on what the break keyword is actually doing.