Java / Control Statements
Does returning a value from the loop body break the loop?
Yes. return statement breaks the loop and returns from the entire method immediately. The only code that will be executed is the finally clause and the release of any synchronized statement.
public class ForLoopReturnExample { public static void main(String[] args) { for (int i = 0; i < 10; i++) { try { if (i == 1) return; System.out.println(" try block. i = " + i); } finally { System.out.println(" finally block i=." + i); } } } }
The same applies to the break statement as well, the finally clause will be executed before exiting the loop.
More Related questions...