1 00:00:00,180 --> 00:00:03,810 Before we start, did you try solving the workbook yourself? 2 00:00:03,840 --> 00:00:09,600 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:10,960 --> 00:00:14,020 Welcome to Workbook 5.4. 4 00:00:14,050 --> 00:00:19,330 The first task is to create a function called Sing Public static. 5 00:00:19,480 --> 00:00:24,160 The function is going to be void because no return type has been specified. 6 00:00:24,190 --> 00:00:30,520 It's simply going to perform the task of singing when it receives a parameter of type int. 7 00:00:31,810 --> 00:00:35,380 And the two lines that it's going to sing are the following. 8 00:00:37,800 --> 00:00:40,050 So first it's going to print. 9 00:00:41,010 --> 00:00:45,480 The number that gets passed in as a placeholder, followed by some text. 10 00:00:47,740 --> 00:00:48,880 Break the string. 11 00:00:49,770 --> 00:00:54,270 Print the number that gets passed in again, followed by another string. 12 00:01:01,570 --> 00:01:02,440 Looking good. 13 00:01:06,320 --> 00:01:08,180 We do the same thing over here. 14 00:01:12,470 --> 00:01:19,580 But you break the string at this placeholder and this time we print whatever number gets passed in minus 15 00:01:19,580 --> 00:01:20,030 one. 16 00:01:26,170 --> 00:01:29,760 So basically, imagine that the number 99 gets passed in. 17 00:01:29,770 --> 00:01:33,850 It would print 99 bottles of beer on the wall, 99 bottles of beer. 18 00:01:34,030 --> 00:01:38,630 Obviously, once you take one down, then you only have 98 bottles of beer left. 19 00:01:38,650 --> 00:01:39,550 All right. 20 00:01:39,730 --> 00:01:44,140 Last thing we need to do is create a for loop that starts at I equals 99. 21 00:01:44,140 --> 00:01:47,860 So for int I is equal to 99. 22 00:01:49,260 --> 00:01:54,210 And the reason this workbook is a bit different from the others is because this time we're starting 23 00:01:54,210 --> 00:01:54,660 high. 24 00:01:54,690 --> 00:01:58,150 We're starting at 99 and we're going to be stepping downwards. 25 00:01:58,170 --> 00:02:03,180 This loop is going to keep running so long as I is greater than zero. 26 00:02:03,210 --> 00:02:09,060 This condition ensures that the last number for which this loop runs ends up equaling one. 27 00:02:09,060 --> 00:02:12,480 And then we can say I minus, minus. 28 00:02:14,850 --> 00:02:21,690 So every time this for loop runs, we're going to minus I by one until it eventually is not greater 29 00:02:21,690 --> 00:02:23,540 than zero and the loop breaks. 30 00:02:23,550 --> 00:02:28,440 So now we can just say sing, passing an eye every single time the loop runs. 31 00:02:28,440 --> 00:02:33,600 Let us put one breakpoint over here and visualize the runtime. 32 00:02:42,960 --> 00:02:46,650 So I start at 99, which is bigger than zero. 33 00:02:46,650 --> 00:02:52,470 And what that's going to do is call the function saying passing in a value of 99, this is just going 34 00:02:52,470 --> 00:02:54,840 to print 99 bottles of beer on the wall. 35 00:02:56,150 --> 00:02:59,660 And then this will print 98 bottles of beer left. 36 00:03:00,780 --> 00:03:04,090 Okay, now we reach the second run in our loop. 37 00:03:04,110 --> 00:03:05,970 So before Iowa's 99. 38 00:03:05,970 --> 00:03:09,630 But after you apply a minus minus, now it's 98. 39 00:03:10,230 --> 00:03:12,300 So we pass in a value of 98. 40 00:03:12,330 --> 00:03:15,180 Now we're singing 98 bottles of beer on the wall. 41 00:03:15,180 --> 00:03:22,260 But after we take one down and pass it around, there are 97 left and this is going to keep going on 42 00:03:22,260 --> 00:03:25,470 and on until I ends up equaling one. 43 00:03:25,470 --> 00:03:28,260 So I'm going to fast forward until it reaches that number. 44 00:03:30,780 --> 00:03:35,340 9876543. 45 00:03:35,340 --> 00:03:42,240 So now I equals to step inside the function prints two bottles of beer, take one down, pass it around. 46 00:03:43,940 --> 00:03:46,010 Okay, so now I equals one. 47 00:03:46,010 --> 00:03:49,880 One is still greater than zero, so the for loop is still going to run. 48 00:03:51,190 --> 00:03:52,720 Stepping inside the function. 49 00:03:52,990 --> 00:03:54,850 One bottle of beer on the wall. 50 00:03:55,790 --> 00:03:58,580 I am minus one zero bottles of beer left. 51 00:04:00,180 --> 00:04:02,430 So at this point I equals one. 52 00:04:02,430 --> 00:04:03,060 I am minus. 53 00:04:03,060 --> 00:04:05,220 Minus is going to update it to zero. 54 00:04:05,250 --> 00:04:07,080 Zero is not greater than zero. 55 00:04:07,080 --> 00:04:09,210 So the for loop is going to break. 56 00:04:10,800 --> 00:04:11,320 And that's it. 57 00:04:11,340 --> 00:04:17,339 Our song starts with 99 bottles of beer on the wall, and it ends with zero bottles of beer. 58 00:04:17,730 --> 00:04:18,540 Perfect.