Do-While Loop in C

Level 256 keystrokes

Uses a do-while loop, which runs its body once before checking the condition, to print n from 1 to 3: "1 2 3".

C code

// The counter n starts at 1
1int n = 1;
// do starts the body, which always runs at least once
2do {
// Prints the current n followed by a space
3 printf("%d ", n);
// Adds 1 to n so the loop can end
4 n++;
// The condition is checked after each pass; when n becomes 4 the loop stops
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.

  • ;× 4
  • "× 2
  • (× 2
  • )× 2
  • +× 2
  • =× 2
  • ,× 1
  • {× 1
  • }× 1
  • %× 1

More C examples at level 2

← C typing practice