Challenge - 5 Problems
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 while loop
What is the output of this C# code snippet?
C Sharp (C#)
int i = 0; while (i < 3) { Console.Write(i); i++; }
Attempts:
2 left
💡 Hint
Remember the loop runs while i is less than 3 and increments i each time.
✗ Incorrect
The loop starts with i=0 and prints i before incrementing. It prints 0, then 1, then 2, then stops because i becomes 3.
❓ Predict Output
intermediate2:00remaining
While loop with break statement
What will be printed by this C# code?
C Sharp (C#)
int count = 0; while (count < 5) { if (count == 3) break; Console.Write(count); count++; }
Attempts:
2 left
💡 Hint
The break stops the loop when count equals 3.
✗ Incorrect
The loop prints 0, 1, 2 and then breaks when count is 3, so 3 and 4 are not printed.
🔧 Debug
advanced2:00remaining
Identify the infinite loop
Which option causes an infinite loop when run?
C Sharp (C#)
int x = 0; while (x < 5) { Console.Write(x); // missing increment }
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
✗ Incorrect
Option A never increments x, so x stays 0 and the condition x < 5 is always true, causing an infinite loop.
❓ Predict Output
advanced2:00remaining
While loop with nested condition
What is the output of this code?
C Sharp (C#)
int i = 0; while (i < 5) { if (i % 2 == 0) Console.Write(i); i++; }
Attempts:
2 left
💡 Hint
Only print i when it is even.
✗ Incorrect
The loop prints i only when i is even (0, 2, 4). Odd numbers are skipped.
❓ Predict Output
expert2:00remaining
While loop with complex condition and post-loop value
What is the value of variable 'result' after this code runs?
C Sharp (C#)
int i = 1; int result = 0; while (i < 10) { if (i % 3 == 0) result += i; i += 2; }
Attempts:
2 left
💡 Hint
Add i to result only if i is divisible by 3, increment i by 2 each time.
✗ Incorrect
i takes values 1,3,5,7,9. Only 3 and 9 are divisible by 3. Sum is 3 + 9 = 12.