Java - Does returning a value break a loop? -
i'm writing code follows following format:
public static boolean isincluded(e element) { node<e> c = head; while (c != null) { if (cursor.getelement().equals(element)) { return true; } c = c.getnext(); } return false; }
the code search element in list of nodes. however, question if while loop find element if-statement says should return true, return true , break loop? furthermore, if break loop carry on through method , still return false, or method completed once value returned?
thanks
yes*
yes, usually (and in case) break out of loop , returns method.
an exception
one exception if there block inside loop , surrounding return statement code in block executed before method returns. block might not terminate - example contain loop or call method never returns. in case wouldn't ever exit loop or method.
while (true) { try { return; // return technically speaking doesn't exit loop. } { while (true) {} // instead gets stuck here. } }
Comments
Post a Comment