0
0
Cprogramming~10 mins

Return inside loops in C - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Return inside loops
Start function
Enter loop
Check condition
Yes
Execute loop body
Return encountered?
YesExit function immediately
No
Next iteration
Back to Check condition
Exit loop
Return at end of function
End function
Back to Check condition
The function starts, enters the loop, and on each iteration checks the condition. If a return statement is hit inside the loop, the function exits immediately without continuing further.
Execution Sample
C
int find_first_even(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        if (arr[i] % 2 == 0) {
            return arr[i];
        }
    }
    return -1;
}
This function returns the first even number found in the array or -1 if none is found.
Execution Table
StepiCondition (i < size)Check arr[i] % 2 == 0ActionReturn Value
100 < 5 = Truearr[0] = 3, 3 % 2 = 1 (False)Continue loopNo return
211 < 5 = Truearr[1] = 7, 7 % 2 = 1 (False)Continue loopNo return
322 < 5 = Truearr[2] = 4, 4 % 2 = 0 (True)Return 44
4---Function exits immediately4
💡 Return inside loop at step 3 causes immediate function exit, loop stops early.
Variable Tracker
VariableStartAfter 1After 2After 3Final
i-012-
Return ValueNoneNoneNone44
Key Moments - 2 Insights
Why does the function stop checking the array after finding the first even number?
Because the return statement inside the loop immediately exits the function, as shown in execution_table step 3 where the function returns 4 and stops.
What happens if no even number is found in the array?
The loop finishes all iterations without hitting the return inside the loop, then the function returns -1 after the loop ends, as shown by the exit_note.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of i when the function returns?
A2
B3
C0
D4
💡 Hint
Check the 'i' column in the row where 'Return 4' happens (step 3).
At which step does the function exit immediately due to return inside the loop?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column for the first return inside the loop.
If the array had no even numbers, what would the function return after the loop?
A0
B-1
CThe last element
DUndefined
💡 Hint
Refer to the exit_note and the last return statement in the code sample.
Concept Snapshot
Return inside loops in C:
- When return is executed inside a loop, function exits immediately.
- Loop stops, no further iterations.
- Useful to return early when condition met.
- If no return in loop, function continues after loop.
- Example: find first even number and return it early.
Full Transcript
This example shows a function that looks for the first even number in an array. It uses a for loop to check each element. When it finds an even number, it returns that number immediately, stopping the loop and the function. If no even number is found, it returns -1 after the loop ends. The execution table traces each step: the loop index i, the condition check, whether the current element is even, and the action taken. The variable tracker shows how i changes each iteration and when the return value is set. Key moments clarify why the function stops early and what happens if no even number is found. The quiz questions help check understanding of when and why the return happens inside the loop.