Before we start, did you try solving the workbook yourself?
If not, please click the link in the resources folder and follow the instructions for this workbook.
Welcome to Workbook 5.4.
The first task is to create a function called Sing Public static.
The function is going to be void because no return type has been specified.
It's simply going to perform the task of singing when it receives a parameter of type int.
And the two lines that it's going to sing are the following.
So first it's going to print.
The number that gets passed in as a placeholder, followed by some text.
Break the string.
Print the number that gets passed in again, followed by another string.
Looking good.
We do the same thing over here.
But you break the string at this placeholder and this time we print whatever number gets passed in minus
one.
So basically, imagine that the number 99 gets passed in.
It would print 99 bottles of beer on the wall, 99 bottles of beer.
Obviously, once you take one down, then you only have 98 bottles of beer left.
All right.
Last thing we need to do is create a for loop that starts at I equals 99.
So for int I is equal to 99.
And the reason this workbook is a bit different from the others is because this time we're starting
high.
We're starting at 99 and we're going to be stepping downwards.
This loop is going to keep running so long as I is greater than zero.
This condition ensures that the last number for which this loop runs ends up equaling one.
And then we can say I minus, minus.
So every time this for loop runs, we're going to minus I by one until it eventually is not greater
than zero and the loop breaks.
So now we can just say sing, passing an eye every single time the loop runs.
Let us put one breakpoint over here and visualize the runtime.
So I start at 99, which is bigger than zero.
And what that's going to do is call the function saying passing in a value of 99, this is just going
to print 99 bottles of beer on the wall.
And then this will print 98 bottles of beer left.
Okay, now we reach the second run in our loop.
So before Iowa's 99.
But after you apply a minus minus, now it's 98.
So we pass in a value of 98.
Now we're singing 98 bottles of beer on the wall.
But after we take one down and pass it around, there are 97 left and this is going to keep going on
and on until I ends up equaling one.
So I'm going to fast forward until it reaches that number.
9876543.
So now I equals to step inside the function prints two bottles of beer, take one down, pass it around.
Okay, so now I equals one.
One is still greater than zero, so the for loop is still going to run.
Stepping inside the function.
One bottle of beer on the wall.
I am minus one zero bottles of beer left.
So at this point I equals one.
I am minus.
Minus is going to update it to zero.
Zero is not greater than zero.
So the for loop is going to break.
And that's it.
Our song starts with 99 bottles of beer on the wall, and it ends with zero bottles of beer.
Perfect.