DeferredReturn
A return
statement that is not at the end of a method does not immediately return,
but it first lets the method finish before returning.
A return statement in the middle of a method doesn't return immediately
A return statement immediately returns from the method
CorrectionHere is what's right.
A return
statement returns immediately.
For example:
public void tick() {
if (value<=0) {
return;
}
value--;
if (value==0) {
ringBell();
}
}
In the above code, if value<=0
then the return
statement executes and returns
from the method immediately
(which means that in this case the subsequent lines are not executed).
Note for advanced students:
The only situation where return
does not return immediately
is if the return
statement is located inside a try
block
with a corresponding finally
block.
ValueHow can you build on this misconception?
This misconception can provide a good opportunity to discuss statement sequences as a key aspect of imperative programming.
This misconception can be an opportunity to point out the fact that
return
statements located inside a try
block
with a corresponding finally
block
indeed do not return immediately.
However, a full-fledged discussion of the details of exception handling
may be too advanced at the time students hold this misconception.