Control flow

4.3 - While loop


We can make a piece of code run multiple times with a while loop. As long as the condition in the while loop is true, it will repeat the code inside.

For example:

int i = 3;

while (i > 0) {
    std::cout << i << std::endl;

    i--; // This is the same as i = i - 1;
}

Will print out:

3
2
1

Task

Your task is to use the while loop to make a program that counts from 1 to 10000.

#include <iostream> int main() { // Start your program here }