Challenge - 5 Problems
Loop Control Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of loop without control
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { int i = 0; while (i < 3) { std::cout << i << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check if the loop variable changes inside the loop.
✗ Incorrect
The variable i is never incremented, so the condition i < 3 is always true, causing an infinite loop printing 0 repeatedly.
🧠 Conceptual
intermediate1:30remaining
Purpose of loop control variables
Why do we need loop control variables in loops?
Attempts:
2 left
💡 Hint
Think about how loops know when to stop.
✗ Incorrect
Loop control variables help the program count how many times the loop has run and decide when to stop looping to avoid infinite loops.
❓ Predict Output
advanced2:00remaining
Effect of missing loop control in for loop
What happens when the loop control update is missing in this for loop?
C++
#include <iostream> int main() { for (int i = 0; i < 3;) { std::cout << i << " "; } return 0; }
Attempts:
2 left
💡 Hint
Is the loop variable changing inside the loop?
✗ Incorrect
The loop variable i is never incremented, so the condition i < 3 remains true forever, causing an infinite loop printing 0 repeatedly.
🔧 Debug
advanced2:00remaining
Identify the cause of infinite loop
Why does this loop run infinitely?
C++
#include <iostream> int main() { int count = 5; while (count > 0) { std::cout << count << " "; } return 0; }
Attempts:
2 left
💡 Hint
Check if the variable controlling the loop changes inside the loop.
✗ Incorrect
The variable count is never changed inside the loop, so the condition count > 0 is always true, causing an infinite loop.
🧠 Conceptual
expert2:30remaining
Why loop control is critical in real-world programs
Which of these best explains why loop control is critical in real-world programming?
Attempts:
2 left
💡 Hint
Think about what happens if a loop never stops.
✗ Incorrect
Proper loop control prevents infinite loops that can freeze programs, consume CPU endlessly, and cause poor user experience or crashes.