Challenge - 5 Problems
Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a loop printing numbers
What is the output of this C code snippet?
C
#include <stdio.h> int main() { for (int i = 1; i <= 3; i++) { printf("%d ", i); } return 0; }
Attempts:
2 left
💡 Hint
Look at the loop start and end values carefully.
✗ Incorrect
The loop starts at 1 and runs while i is less than or equal to 3, printing 1, 2, and 3 with spaces.
🧠 Conceptual
intermediate1:30remaining
Why use loops instead of repeated code?
Why do programmers use loops instead of writing the same code many times?
Attempts:
2 left
💡 Hint
Think about what happens if you want to change repeated code.
✗ Incorrect
Loops help avoid repeating the same code many times, making programs shorter and easier to update.
❓ Predict Output
advanced2:30remaining
Output of nested loops
What is the output of this C code with nested loops?
C
#include <stdio.h> int main() { for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 2; j++) { printf("%d%d ", i, j); } } return 0; }
Attempts:
2 left
💡 Hint
The outer loop controls the first digit, inner loop the second.
✗ Incorrect
The outer loop runs i=1 then i=2; for each i, inner loop runs j=1 and j=2, printing pairs in order.
❓ Predict Output
advanced2:00remaining
Output of a while loop with break
What is the output of this C code using a while loop and break?
C
#include <stdio.h> int main() { int i = 1; while (i <= 5) { if (i == 3) { break; } printf("%d ", i); i++; } return 0; }
Attempts:
2 left
💡 Hint
The break stops the loop when i equals 3.
✗ Incorrect
The loop prints i while i is less than 3; when i is 3, break stops the loop before printing.
🧠 Conceptual
expert3:00remaining
Why loops are essential for repetitive tasks
Which statement best explains why loops are essential in programming for repetitive tasks?
Attempts:
2 left
💡 Hint
Think about how loops help when you want to repeat actions many times.
✗ Incorrect
Loops let programmers repeat code easily, which saves time and reduces mistakes compared to copying code multiple times.