Bird
Raised Fist0
Cprogramming~20 mins

Return inside loops in C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Return Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
A-1
B0
C4
D2
Attempts:
2 left
💡 Hint
The function returns as soon as i equals 2 inside the loop.
❓ Predict Output
intermediate
2: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;
}
A0
B3
C1
D5
Attempts:
2 left
💡 Hint
The function returns when n equals 3 during the countdown.
🔧 Debug
advanced
2: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;
}
ABecause the loop never runs
BBecause sum is reset to 0 when i is 5
CBecause the return 0 inside the loop stops the function when i is 5
DBecause sum is returned before the loop starts
Attempts:
2 left
💡 Hint
Look at the return statement inside the loop and when it triggers.
❓ Predict Output
advanced
2: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;
}
A5
B6
C4
D0
Attempts:
2 left
💡 Hint
Find the first pair (i,j) where i*j equals 4 and return their sum.
❓ Predict Output
expert
3: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;
}
A1
B-1
C0
D3
Attempts:
2 left
💡 Hint
Remember that return exits the function immediately, continue skips to next loop iteration, and break exits the loop.

Practice

(1/5)
1. What happens when a return statement is executed inside a loop in a C function?
easy
A. The program crashes due to an error.
B. The loop skips the current iteration and continues.
C. The loop finishes all iterations before returning.
D. The function immediately stops and returns the specified value.

Solution

  1. Step 1: Understand the role of return inside a function

    The return statement immediately ends the function and sends back a value.
  2. Step 2: Effect of return inside a loop

    When return is inside a loop, the function stops as soon as return is executed, even if the loop is not finished.
  3. Final Answer:

    The function immediately stops and returns the specified value. -> Option D
  4. Quick Check:

    Return inside loop = immediate function exit [OK]
Hint: Return inside loop stops function immediately [OK]
Common Mistakes:
  • Thinking return only exits the loop, not the function
  • Assuming loop completes before return
  • Confusing return with continue or break
2. Which of the following is the correct syntax to return the value 5 inside a for loop in C?
easy
A. for(int i=0; i<3; i++) { return 5; }
B. for(int i=0; i<3; i++) return(5)
C. for(int i=0; i<3; i++) { return; 5; }
D. for(int i=0; i<3; i++) { return 5 }

Solution

  1. Step 1: Check syntax of return statement

    Return must be followed by a value and a semicolon inside braces if used.
  2. Step 2: Analyze each option

    for(int i=0; i<3; i++) { return 5; } uses correct syntax with braces and semicolon. for(int i=0; i<3; i++) return(5) misses semicolon, invalid. for(int i=0; i<3; i++) { return; 5; } has return without value then 5; which is invalid. for(int i=0; i<3; i++) { return 5 } misses semicolon after return 5.
  3. Final Answer:

    for(int i=0; i<3; i++) { return 5; } -> Option A
  4. Quick Check:

    Return syntax needs value and semicolon [OK]
Hint: Return must have value and semicolon inside braces [OK]
Common Mistakes:
  • Omitting semicolon after return
  • Placing code after return on same line
  • Using return without value in non-void function
3. What is the output of this C function?
int findFirstEven(int arr[], int size) {
  for(int i = 0; i < size; i++) {
    if(arr[i] % 2 == 0) {
      return arr[i];
    }
  }
  return -1;
}

int main() {
  int nums[] = {3, 7, 8, 5};
  printf("%d", findFirstEven(nums, 4));
  return 0;
}
medium
A. 3
B. 7
C. 8
D. -1

Solution

  1. Step 1: Trace the loop to find first even number

    The array is {3, 7, 8, 5}. Loop checks each element: 3 (odd), 7 (odd), 8 (even).
  2. Step 2: Return when first even found

    At element 8, condition is true, so function returns 8 immediately, skipping rest.
  3. Final Answer:

    8 -> Option C
  4. Quick Check:

    Return stops loop at first even number 8 [OK]
Hint: Return inside loop returns first matching value [OK]
Common Mistakes:
  • Assuming loop continues after return
  • Returning wrong element index
  • Confusing return value with loop index
4. Identify the error in this C function that tries to return the first negative number from an array:
int firstNegative(int arr[], int n) {
  for(int i=0; i
medium
A. The function has no return statement.
B. The function returns 0 too early, skipping rest of array.
C. The loop syntax is incorrect.
D. The function returns negative numbers incorrectly.

Solution

  1. Step 1: Analyze the loop and return logic

    The else clause returns 0 immediately if the first element is not negative, so the loop never checks further elements.
  2. Step 2: Understand why this is a problem

    This causes the function to stop early and miss negative numbers later in the array.
  3. Final Answer:

    The function returns 0 too early, skipping rest of array. -> Option B
  4. Quick Check:

    Return in else exits function too soon [OK]
Hint: Avoid return in else inside loop to check all elements [OK]
Common Mistakes:
  • Returning inside else causes early exit
  • Not using braces causing confusion
  • Assuming loop checks all elements
5. You want to write a C function that returns the index of the first prime number in an array, or -1 if none found. Which code snippet correctly uses return inside a loop to achieve this?
hard
A. for(int i=0; i
B. for(int i=0; i
C. for(int i=0; i
D. for(int i=0; i

Solution

  1. Step 1: Understand the goal

    We want to return the index of the first prime number found, or -1 if none.
  2. Step 2: Analyze each option's logic

    for(int i=0; i
  3. Final Answer:

    for(int i=0; i -> Option A
  4. Quick Check:

    Return index on first prime, else -1 after loop [OK]
Hint: Return inside loop on condition, return default after loop [OK]
Common Mistakes:
  • Returning wrong value inside loop
  • Using break without return
  • Returning default inside loop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard