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 while Loop

Imagine you need to print the numbers from 1 to 100. You could write std::cout a hundred times, but it would take ages, and changing anything later would be even worse. Programs repeat work like this all the time: they go through every student in a class, check every product in a shop, or redraw a game screen 60 times a second. That is what loops are for. A loop is a piece of code that runs again and again.

The simplest loop is while. It repeats a block of code while its condition is true:

int number = 1;
while (number <= 5) {
    std::cout << number << std::endl;
    number++;
}

This prints:

1
2
3
4
5

A while loop looks like an if: the keyword, a condition in parentheses and a block of code in curly braces. The difference is that if runs its block at most once, but while keeps coming back to the condition. Step by step:

  1. The program creates number with the value 1.
  2. It checks the condition number <= 5. It is true, so the program runs the block: it prints 1, and number++ increases number to 2.
  3. It goes back to the condition. 2 <= 5 is still true, so it prints 2 and increases number to 3.
  4. And so on, until number is 6. Then the condition is false, the loop ends, and the program continues with the code after the loop's closing brace }.

Each run of the block is called an iteration. This loop has five iterations. To print the numbers up to 100, you would only change 5 to 100.

The code after the loop runs only once, when the loop has finished:

int number = 1;
while (number <= 3) {
    std::cout << number << std::endl;
    number++;
}
std::cout << "Done" << std::endl;
1
2
3
Done

Notice the line number++. Something in the block must change the variable in the condition, so that the condition becomes false at some point. Without it, the loop would never end. You'll see what happens then in the next exercise.

Instructions

  1. 1

    Delete the three lines that print 1, 2 and 3. In their place, create a variable number of type int with the value 10. The loop will count with it.

  2. 2

    Below it, write a while loop that runs while number is less than or equal to 20. Inside the loop, print number on its own line and then increase it by 1. The program prints the numbers from 10 to 20, each on its own line.

  3. 3

    After the loop, below its closing brace }, print After the loop: and the value of number. The last line of the output is After the loop: 21. The variable ends up as 21, not 20, because the loop stops only when the condition is false.

Back

1/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.