While Loop with Break in C++

Level 270 keystrokes

Starts an endless while (true) loop that prints n and leaves it with break once n reaches 3, so 1, 2 and 3 are printed.

C++ code

// Initializes the counter n with 1
1int n = 1;
// The condition is always true, so only break can end this loop
2while (true) {
// Prints the current value of n
3 cout << n << endl;
// After 3 has been printed, break leaves the loop
4 if (n == 3) break;
// Otherwise increases n by 1 and repeats
5 n++;
6}

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.

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

More C++ examples at level 2

← C++ typing practice