Challenge - 5 Problems
Do-While Loop Mastery
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 snippet?
C Sharp (C#)
int count = 0; do { Console.Write(count + " "); count++; } while (count < 3);
Attempts:
2 left
💡 Hint
Remember, do-while executes the loop body before checking the condition.
✗ Incorrect
The loop starts with count = 0, prints it, then increments. It repeats while count < 3, so it prints 0, 1, and 2.
❓ Predict Output
intermediate2:00remaining
Do-while loop with condition false initially
What will this C# code print?
C Sharp (C#)
int x = 5; do { Console.WriteLine("Inside loop"); x++; } while (x < 5);
Attempts:
2 left
💡 Hint
Do-while runs the loop body at least once even if the condition is false.
✗ Incorrect
Even though x is 5 and condition x < 5 is false, the loop body runs once before checking condition.
🔧 Debug
advanced2:00remaining
Identify the infinite loop in do-while
Which option causes an infinite loop when run?
C Sharp (C#)
int i = 0; do { Console.Write(i + " "); } while (i < 3);
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop body.
✗ Incorrect
Option D never changes i inside the loop, so condition i < 3 is always true, causing infinite loop.
❓ Predict Output
advanced2:00remaining
Value of variable after do-while loop
What is the value of variable 'sum' after this code runs?
C Sharp (C#)
int sum = 0; int n = 1; do { sum += n; n++; } while (n <= 5);
Attempts:
2 left
💡 Hint
Sum numbers from 1 to 5 inclusive.
✗ Incorrect
The loop adds 1+2+3+4+5 = 15 to sum.
🧠 Conceptual
expert2:00remaining
Do-while loop execution guarantee
Which statement best describes the execution behavior of a do-while loop in C#?
Attempts:
2 left
💡 Hint
Think about how do-while differs from while loop.
✗ Incorrect
Do-while loops always run the body once before checking the condition.