Recall & Review
beginner
What happens when a
return statement is executed inside a loop in C?The function immediately exits and returns the specified value. The loop and the rest of the function do not continue.
Click to reveal answer
beginner
Can a
return statement inside a loop be used to exit the loop early?Yes, but it exits the entire function, not just the loop. To exit only the loop, use
break instead.Click to reveal answer
beginner
Consider this code snippet:<br>
for (int i = 0; i < 5; i++) {
if (i == 3) return i;
}<br>What will the function return?The function will return 3 as soon as
i equals 3, exiting the loop and function immediately.Click to reveal answer
intermediate
Is it good practice to use
return inside loops? Why or why not?It can be useful for early exit when a result is found, but overusing it can make code harder to read and debug.
Click to reveal answer
beginner
What is the difference between
return and break inside a loop?return exits the entire function immediately, while break only exits the current loop and continues the function.Click to reveal answer
What does a
return statement inside a loop do in C?✗ Incorrect
A
return exits the whole function immediately, not just the loop.Which statement should you use to exit only the loop but continue the function?
✗ Incorrect
break exits the loop but the function continues.If a
return is inside a loop, what happens to the rest of the loop iterations?✗ Incorrect
The function ends immediately, so no more loop iterations happen.
Why might using
return inside loops be discouraged?✗ Incorrect
Early returns can make code flow harder to follow.
What will this code return?<br>
for (int i = 0; i < 10; i++) {
if (i == 5) return 100;
}
return 0;✗ Incorrect
When i equals 5, the function returns 100 immediately.
Explain what happens when a
return statement is used inside a loop in C.Think about what return does in a function.
You got /3 concepts.
Describe the difference between using
return and break inside a loop.One stops the whole function, the other just the loop.
You got /3 concepts.