Java: break, continue, return in Loops

Although this is pretty basic Java functionality I often have to look this up, because I confound these calls, esp. break and continue. So this is my cheat sheet:

Break

for (..) {
  ..
  // leave loop immediately
  break;
  ..
}

Continue

for (..) {
  ..
  // continue loop with next iteration
  continue;
  ..
}

Return

for (..) {
  ..
  // leave calling method
  return;
  ..
}

One thought on “Java: break, continue, return in Loops

  1. Considering that break/continue are in fact GOTO statements, I like the fact that you do not use them that often thus do not remember them ;-) Esp. break/continue with a label name always make me suspicious and I look for ways to change the method/design.

Comments are closed.