0
0
Goprogramming~10 mins

Return inside loops in Go - 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 and enters a loop. Each iteration checks a condition. If a return statement runs inside the loop, the function exits immediately, skipping remaining iterations.
Execution Sample
Go
func findFirstEven(nums []int) int {
    for _, n := range nums {
        if n%2 == 0 {
            return n
        }
    }
    return -1
}
This function loops through numbers and returns the first even number it finds, stopping the loop immediately.
Execution Table
StepIterationCurrent number (n)Condition (n%2==0)ActionReturn Value
113falseContinue loopnone
227falseContinue loopnone
334trueReturn 4 and exit function4
💡 Return executed at iteration 3 when n=4, function exits immediately.
Variable Tracker
VariableStartAfter 1After 2After 3 (Return)
nundefined374
return valuenonenonenone4
Key Moments - 2 Insights
Why does the loop stop immediately when return is executed inside it?
Because return exits the entire function immediately, skipping all remaining loop iterations as shown in execution_table row 3.
What happens if no number satisfies the condition inside the loop?
The loop finishes all iterations without returning, then the function returns the value after the loop, here -1 (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'n' at step 2?
A4
B3
C7
Dnone
💡 Hint
Check the 'Current number (n)' column at step 2 in the execution_table.
At which iteration does the function return a value?
A3
B2
C1
DNever
💡 Hint
Look at the 'Return Value' column and find when it changes from 'none' to a number.
If the input slice had no even numbers, what would happen to the return value?
AReturn the first number anyway
BReturn -1 after the loop finishes
CReturn 0 immediately
DThe function would never return
💡 Hint
Refer to the code in execution_sample where return -1 is after the loop.
Concept Snapshot
Return inside loops in Go:
- A return inside a loop exits the entire function immediately.
- Loop stops running once return executes.
- If no return in loop, function can return after loop.
- Useful to find and return first matching item quickly.
Full Transcript
This example shows a Go function that loops through a list of numbers. Each number is checked if it is even. When the function finds the first even number, it returns that number immediately, stopping the loop and the function. If no even number is found, the function returns -1 after the loop finishes. The execution table traces each step, showing the current number, condition check, and when the return happens. The variable tracker shows how the variable 'n' changes each iteration and when the return value is set. Key moments clarify why the loop stops on return and what happens if no return occurs inside the loop. The quiz tests understanding of these steps and outcomes.