Do-While Loop in C

Level 381 keystrokes

Uses a do-while loop to print powers of 2 up to 8.

C code

// Start at 2^0 = 1
1int num = 1;
// The body runs once before the condition is checked
2do {
// Print the current power of 2
3 printf("%d ", num);
// Double it: 1 → 2 → 4 → 8 → 16
4 num *= 2;
// When num becomes 16 the condition fails, so 16 is never printed
5} while (num <= 8);
// End the line: 1 2 4 8
6printf("\n");

Output

1 2 4 8
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
  • (× 3
  • )× 3
  • =× 3
  • ,× 1
  • {× 1
  • }× 1
  • *× 1
  • \× 1

More C examples at level 3

← C typing practice