Challenge - 5 Problems
Return Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of return inside a for loop
What is the output of this C program?
C
#include <stdio.h> int test() { for (int i = 0; i < 5; i++) { if (i == 2) { return i; } } return -1; } int main() { printf("%d\n", test()); return 0; }
Attempts:
2 left
💡 Hint
The function returns as soon as i equals 2 inside the loop.
✗ Incorrect
The loop runs from i=0 to i=4. When i reaches 2, the return statement exits the function immediately with value 2.
❓ Predict Output
intermediate2:00remaining
Return inside while loop behavior
What will this program print?
C
#include <stdio.h> int count_down(int n) { while (n > 0) { if (n == 3) { return n; } n--; } return 0; } int main() { printf("%d\n", count_down(5)); return 0; }
Attempts:
2 left
💡 Hint
The function returns when n equals 3 during the countdown.
✗ Incorrect
The while loop decreases n from 5 down to 1. When n is 3, the function returns 3 immediately.
🔧 Debug
advanced2:30remaining
Why does this function return early?
Consider this function. Why does it return 0 instead of 10?
C
#include <stdio.h> int sum_until_five() { int sum = 0; for (int i = 1; i <= 10; i++) { if (i == 5) { return 0; } sum += i; } return sum; } int main() { printf("%d\n", sum_until_five()); return 0; }
Attempts:
2 left
💡 Hint
Look at the return statement inside the loop and when it triggers.
✗ Incorrect
The function returns 0 immediately when i reaches 5, so it never sums beyond 4.
❓ Predict Output
advanced2:30remaining
Return inside nested loops
What does this program print?
C
#include <stdio.h> int nested_loop() { for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i * j == 4) { return i + j; } } } return 0; } int main() { printf("%d\n", nested_loop()); return 0; }
Attempts:
2 left
💡 Hint
Find the first pair (i,j) where i*j equals 4 and return their sum.
✗ Incorrect
The nested loops check i from 1 to 3 and j from 1 to 3. The first time i*j == 4 is when i=2 and j=2, so it returns 2+2=4.
❓ Predict Output
expert3:00remaining
Return inside loop with break and continue
What is the output of this program?
C
#include <stdio.h> int tricky() { for (int i = 0; i < 5; i++) { if (i == 2) { continue; } if (i == 3) { break; } if (i == 1) { return i; } } return -1; } int main() { printf("%d\n", tricky()); return 0; }
Attempts:
2 left
💡 Hint
Remember that return exits the function immediately, continue skips to next loop iteration, and break exits the loop.
✗ Incorrect
When i=0, no condition triggers. When i=1, return i exits function with 1. So output is 1.