Break Statement in C++

Level 271 keystrokes

Loops i from 1 to 10 but leaves the loop with break as soon as i reaches 5, so only 1 to 4 are printed.

C++ code

// Would count i from 1 to 10 if nothing stopped it
1for (int i = 1; i <= 10; i++) {
// When i is 5, break ends the whole loop immediately
2 if (i == 5) break;
// Reached only for i = 1, 2, 3 and 4, which are printed
3 cout << i << endl;
4}

Output

1
2
3
4
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
  • =× 4
  • (× 2
  • )× 2
  • +× 2
  • {× 1
  • }× 1

More C++ examples at level 2

← C++ typing practice