Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

The continue Statement

The continue statement is the smaller sibling of break. It doesn't end the whole loop, only the current iteration: it skips the rest of the block, and the loop goes straight on to the next iteration.

This loop prints the numbers from 1 to 10, but skips the multiples of 3:

for (int number = 1; number <= 10; number++) {
    if (number % 3 == 0) {
        continue;
    }
    std::cout << number << std::endl;
}

This prints:

1
2
4
5
7
8
10

For 3, 6 and 9, the condition is true, and continue skips the line that prints. The step number++ still runs, and the loop carries on with the next number as usual.

The continue statement usually sits at the beginning of the block and filters out the elements you don't want to deal with: “this one doesn't interest me, on to the next one”. The rest of the block then only handles the interesting ones.

Be careful with continue in a while loop. It jumps straight back to the condition, so if the line that increases the counter is below continue, it is skipped too. The counter stops changing, and the loop gets stuck. In a for loop, this can't happen: its step runs after every iteration, including one that ended with continue, and the range-based for moves on to the next element by itself.

Instructions

  1. 1

    Your phone counts your steps every day. The vector dailySteps holds the counts for six days, and -1 means the phone was switched off that day. Write a for loop over dailySteps. At the start of its block, write an if that skips the value with continue when it is -1.

  2. 2

    After the if, still inside the loop, print the value. The output is 4200, 6100, 3800 and 7500, each on its own line.

  3. 3

    Now work out the average number of steps on the days that were measured. Before the loop, create the variables total and days, both with the value 0. In the loop, below the if with continue, add the value to total and increase days by 1. After the loop, print total / days as Average: 5400. Divide by days, not by the size of the vector: the skipped days don't count.

Back

11/13

  1. 1. The while Loop
  2. 2. Infinite Loops
  3. 3. The for Loop
  4. 4. Looping over a Vector
  5. 5. Loops and Text
  6. 6. Sum and Average
  7. 7. Counting
  8. 8. The Largest Value
  9. 9. Building a New Vector
  10. 10. The break Statement
  11. 11. The continue Statement
  12. 12. Nested Loops
  13. 13. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.