Learn
Loops
99 Bottles

In the last exercise, we saw an example of an incrementing for loop so here we are going to show you how to write a for loop where the counter goes down. When we know exactly how many times we want to iterate (or when we are counting), we can use a for loop instead of a while loop:

Incrementing for loop:

for (int i = 0; i < 20; i++) { // Statements }

Decrementing for loop:

for (int i = 20; i > 0; i--) { // Statements }

Instructions

1.

Write a 99bottles.cpp program that prints the verses of the “99 Bottles” song. Each stanza goes something like this:

i bottles of pop on the wall. Take one down and pass it around. i-1 bottles of pop on the wall.

Hint: Use a decrementing for loop!

Folder Icon

Take this course for free

Already have an account?