Challenge - 5 Problems
Do–while Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple do–while loop
What is the output of this C code?
C
int main() { int i = 1; do { printf("%d ", i); i++; } while (i <= 3); return 0; }
Attempts:
2 left
💡 Hint
Remember, do–while executes the loop body before checking the condition.
✗ Incorrect
The loop starts with i=1, prints it, then increments i. It repeats while i <= 3, so it prints 1, 2, and 3.
❓ Predict Output
intermediate2:00remaining
Do–while loop with initial false condition
What will this program print?
C
int main() { int i = 5; do { printf("%d ", i); i++; } while (i < 5); return 0; }
Attempts:
2 left
💡 Hint
The loop body runs once before the condition is checked.
✗ Incorrect
Even though i < 5 is false initially, the do–while loop runs the body once, printing 5.
❓ Predict Output
advanced2:30remaining
Nested do–while loops output
What is the output of this nested do–while loop code?
C
int main() { int i = 1, j; do { j = 1; do { printf("%d%d ", i, j); j++; } while (j <= 2); i++; } while (i <= 2); return 0; }
Attempts:
2 left
💡 Hint
The inner loop runs fully for each iteration of the outer loop.
✗ Incorrect
For i=1, j runs 1 to 2 printing 11 and 12. Then i=2, j runs 1 to 2 printing 21 and 22.
❓ Predict Output
advanced2:00remaining
Do–while loop with break statement
What will this program print?
C
int main() { int i = 1; do { if (i == 3) break; printf("%d ", i); i++; } while (i <= 5); return 0; }
Attempts:
2 left
💡 Hint
The break stops the loop when i equals 3 before printing.
✗ Incorrect
When i is 3, the loop breaks before printing, so only 1 and 2 are printed.
❓ Predict Output
expert2:30remaining
Value of variable after do–while loop with complex condition
What is the value of variable x after this code runs?
C
int main() { int x = 0, y = 5; do { x += y; y--; } while (y > 0); printf("%d", x); return 0; }
Attempts:
2 left
💡 Hint
Sum y from 5 down to 1 added to x.
✗ Incorrect
The loop adds 5 + 4 + 3 + 2 + 1 = 15 to x before y becomes 0 and loop stops.