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 }



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!

If you notice the Run button spinning continuously or a “Lost connection to Codecademy” message in this exercise, you may have an infinite loop! If the stop condition for our loop is never met, we will create an infinite loop which stops our program from running anything else. To exit out of an infinite loop in an exercise, refresh the page — then fix the code for your loop.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?