C/C++ Arena

Step 2 of 6

for loops

A for loop puts the three parts of a counting loop on one line:

for (int i = 0; i < 5; i++) {
    printf("%d ", i);
}
// 0 1 2 3 4
  1. init int i = 0 runs once
  2. condition i < 5 is checked before each round
  3. update i++ runs after each round

Your turn: read n and print the squares from 1 to n on one line, separated by spaces. For n = 4: 1 4 9 16.

Previous: while loops Next: Accumulating a total