I'm working on a project for school where I have many nested loops in a method. I can verify that my code is reaching the "return true;" statement before a "return false;" using way too many System.out.println statements.
Is there any case where return does not immediately exit the method. In my code it reaches the "return true;", but then continues working through the method.
Thanks - first post, but long time student from other questions
EDIT: Show my code - This is for a UC Berkeley project so they aren't allowing us to post our full code. Here is the important parts.
There are no "try statements" in my code. This is the very end of my method which has a ton of loops (while & for) loops.
if (x == 7 && turn == 5){
System.out.println("TRUE RESULT");
return true;
}
nextVictory(turn, x, y, color,z);
alive = false;
}
}
}
}
System.out.println("FALSE RESULT");
return false;
}
I have print statement to verify where I'm at. Here are the results:
TRUE RESULT
FALSE RESULT
FALSE RESULT
FALSE RESULT
FALSE RESULT
解决方案
Is there any case where return does not immediately exit the method?
The only case that comes to mind is if the return statement were inside a try-finally block. The finally block would still get executed.
Another case that could exhibit similar behaviour is if your method is recursive. In that case, a return statement in a nested call would actually return to the caller. However, if the caller is the method itself, this could have the appearance of the method continuing to execute past the return.