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;
  ..
}

Advertisements
This entry was posted in howtos and tagged , , , , , , , . Bookmark the permalink.

One Response to Java: break, continue, return in Loops

  1. Peter Kofler says:

    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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s