Java Statements: Break, Continue & Return

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Sometimes we need to get out of certain situations, keep going on the path we're on, or hand the keys to someone. Java provides control statements that allow us to stop processing, continue processing, or return control back to someone else. This lesson will define these statements and provide working code examples.

Break Out, Keep Going, or Give up the Keys?

Sometimes in Java we need to break out of code that is running, or we need to keep processing. Sometimes we have to give the keys out and give them back. The Java control statements that allow each respective action are called break, continue, and return. In Java terms, these are called branching statements because they take us away from the current path we are on. We will discuss each one and provide some code examples for their use.


The following example shows the break statement in action, in the context of a main menu. If the user types in a certain value, a specific part of the code is processed. In each case, we want to break out when done. There's no need to process any code after we've processed each block. Break is a very useful branching statement in such a menu, because it allows the user to return to the main.

switch (option) {
  case 1:
   System.out.println("Option 1");
   System.out.println("Outta here!");

Another option is to break if some condition is met during processing. If there is the possibility that a loop or other block of code could get out of hand, we can check our values and break out if need be.

The following shows how you could branch out of a loop if the value gets past the point we want. When using loops, it's possible that variables get assigned values that are out of our expected range; it's a good idea to have something in place to catch them if the program runs amok or crashes.

for(int i = 0; i < 15; i++) {
  if(i == 5) {

Note that the break will only break out of the current loop. That is, if we had nested loops, the break only exits the current loop. This is actually where the break can be used to great effect: Running nested loops can get complicated, and sometimes out of hand. Breaking out of one of the inner loops may be effective in having a workable program.

The only statements that contain break are switch, while, do, and for. These are loops that repeat a set of instructions until a condition is met. With the power of the branching statements, however, we can get out of those loops if needed.


Continue does what it implies: it keeps going. Usually you want the program to continue on until something happens without having to tell it! Then again, computers only do what humans tell them.

Let's say we are in a 'while', 'do', or 'for' loop, and there is a statement to use break at some point. If the code DOESN'T break at that point, it makes sense to tell Java to keep going. Using continue is really a matter of style, because if there is an if statement that breaks out if a value is true, then Java will just keep going if that value is false. But it is also important to write code that relays your true intentions: Someone else reading your statements should be able to figure out your intentions.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account