How does numberOfBottles increment in BeerSong?

Hi, I’m working my way through BeerSong and it all makes sense except the last printf statement that prints the “put a bottle in the recycling bin” message. It prints 4 times but I don’t see how the variable numberOfBottles is being incremented. Can anyone help?
Thanks!

1 Like

In the main function, you are calling the function singSongFor(4). The (4) means 4 times. The argument for singSongFor is ‘numberOfBottles’.

In the else it is reducing the numberOfBottles before the recycle statement with ‘singSongFor(oneFewer)’. After all ‘numberOfBottles’ hit zero, it calls the printf(“Put a bottle in the recycling, %d empty bottles in the bin.\n”, numberOfBottles);

Test it. Move the statement before the ‘singSongFor(oneFewer)’ and see what you get. Comment out one below.

Howdy, and thanks for your reply.

I get that we’re calling the singSongFor function 4 times and I also see how we decrement numberOfBottles down by the statement “int oneFewer = numberOfBottles - 1;”. I see how that gets us down from 4 to 0 and then the ‘if’ statement should print “no more bottles”.

I don’t see where we increment numberOfBottles from 0 back up to 4.

What am I missing?

Thanks.

1 Like

Cool, I hear ya. But please take the opportunity to mess with the code to familiarize yourself.

It prints 4x because numberOfBottles is being called in printf . . . printf(“Put a bottle in the recycling, %d empty bottles in the bin.\n”, numberOfBottles);

Change the function singSongFor(4); add 10 times.

Still not getting this, but appreciate your comments and tips. I’m going to continue to the chapter challenge and then maybe come back to this.
Thanks,

1 Like