SteelReed

Search

Search posts, questions…

Java CoreQuestion 7 / 43

Junior

How do while, do-while and for differ? What is the difference between break, continue, return and System.exit?

Short answer

while checks first and may run 0 times; do-while runs at least once; for is for a known number of iterations.

skip the restleave nearest loopleave the methodstop the JVMcontinueNext iterationbreakStatement after loopreturnBack to callerSystem.exitShutdown hooks runfinally does not

3 points to say:

  1. continue skips a turn, break leaves the nearest loop.
  2. return leaves the whole method, finally still runs.
  3. System.exit stops the whole JVM, finally does not run.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.