Return inside loops in C - Time & Space Complexity
When a return statement is inside a loop, it can stop the loop early. This affects how long the code runs.
We want to know how the running time changes as the input gets bigger.
Analyze the time complexity of the following code snippet.
int findFirstPositive(int arr[], int n) {
for (int i = 0; i < n; i++) {
if (arr[i] > 0) {
return arr[i];
}
}
return -1;
}
This code looks for the first positive number in an array and returns it immediately when found.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through the array elements one by one.
- How many times: Up to n times, but can stop earlier if a positive number is found.
Execution depends on where the first positive number appears.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Between 1 and 10 checks |
| 100 | Between 1 and 100 checks |
| 1000 | Between 1 and 1000 checks |
Pattern observation: The loop may stop early, so the time can be less than n, but in the worst case it checks all elements.
Time Complexity: O(n)
This means the time grows linearly with the size of the input in the worst case.
[X] Wrong: "Because the return stops the loop early, the time is always constant."
[OK] Correct: Sometimes the positive number is near the start, but if it is at the end or missing, the loop runs through all elements.
Understanding how early returns affect loops helps you explain code efficiency clearly and shows you think about best and worst cases.
"What if the return statement was replaced with a variable assignment and the loop always ran to the end? How would the time complexity change?"
Practice
return statement is executed inside a loop in a C function?Solution
Step 1: Understand the role of return inside a function
Thereturnstatement immediately ends the function and sends back a value.Step 2: Effect of return inside a loop
Whenreturnis inside a loop, the function stops as soon asreturnis executed, even if the loop is not finished.Final Answer:
The function immediately stops and returns the specified value. -> Option DQuick Check:
Return inside loop = immediate function exit [OK]
- Thinking return only exits the loop, not the function
- Assuming loop completes before return
- Confusing return with continue or break
Solution
Step 1: Check syntax of return statement
Return must be followed by a value and a semicolon inside braces if used.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.Final Answer:
for(int i=0; i<3; i++) { return 5; } -> Option AQuick Check:
Return syntax needs value and semicolon [OK]
- Omitting semicolon after return
- Placing code after return on same line
- Using return without value in non-void 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;
}Solution
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).Step 2: Return when first even found
At element 8, condition is true, so function returns 8 immediately, skipping rest.Final Answer:
8 -> Option CQuick Check:
Return stops loop at first even number 8 [OK]
- Assuming loop continues after return
- Returning wrong element index
- Confusing return value with loop index
int firstNegative(int arr[], int n) {
for(int i=0; iSolution
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.Step 2: Understand why this is a problem
This causes the function to stop early and miss negative numbers later in the array.Final Answer:
The function returns 0 too early, skipping rest of array. -> Option BQuick Check:
Return in else exits function too soon [OK]
- Returning inside else causes early exit
- Not using braces causing confusion
- Assuming loop checks all elements
return inside a loop to achieve this?Solution
Step 1: Understand the goal
We want to return the index of the first prime number found, or -1 if none.Step 2: Analyze each option's logic
for(int i=0; iFinal Answer:
for(int i=0; i -> Option AQuick Check:
Return index on first prime, else -1 after loop [OK]
- Returning wrong value inside loop
- Using break without return
- Returning default inside loop
Ready to prove it?
Pass the test and this pattern moves unknown
