Java Loops: break
break
can be used to exit a loop in the middle of an iteration:
Example: Simple REPL
while (true) {
String command = System.console().readLine();
if (command.equals("quit")) {
break;
}
process(command);
}
break
works precisely the same in for
and do
…while
loops.
Nested Loops
By default break
only breaks the immediately enclosing loop. To break out of an outer loop, used labelled statements.
Example: Breaking out of an inner vs an outer loop.
Comments
Be the first to comment!