Do-While Loop in C++

Level 257 keystrokes

Uses a do-while loop, which runs its body before checking the condition, to print 1, 2 and 3.

C++ code

// Initializes the counter n with 1
1int n = 1;
// do starts the body, which always runs at least once
2do {
// Prints the current value of n
3 cout << n << endl;
// Increases n by 1 for the next pass
4 n++;
// The condition is checked after the body; the loop ends when n becomes 4
5} while (n <= 3);

Output

1
2
3
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 5
  • ;× 4
  • +× 2
  • =× 2
  • (× 1
  • )× 1
  • {× 1
  • }× 1

More C++ examples at level 2

← C++ typing practice