Do-While Loops in Java: Syntax & Example

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.

Programming is about repetitive tasks: The Java Do-While loop is great for performing a specific set of instructions while a condition is true. In this lesson, we'll cover the syntax and provide some real-world code examples.

Do-While

Java, as most programming languages, fully supports looping. That is, performing repetitive tasks until a certain condition is met. Some loops run a set number of times, others run until a certain condition is met. The Do-While loop runs while a specific condition is true. Sometimes it's explained the opposite: running until the condition is false. However, we will refer to the condition running WHILE the condition is TRUE; after all, it's a Do-While loop!

Here's the key idea to this: Many loops start out by checking the condition at the beginning of the loop. The Do-While loop checks at the end. That way, all of the lines of code are run through and THEN Java checks to make sure if it can be done. Think of a child running through a sprinkler. They can run through until the sun dips below the neighbor's house, but they get to run through first, then check the location of the sun.

Syntax

The basic syntax for the Do-While can be a little confusing, so let's break it down.

do {
  //commands
}
while (condition);

Remember that the condition is tested at the end of the statement; this is why the while keyword is placed at the end. It does look a little odd to have the keyword after the final curly bracket. However, we are letting the kid run through the sprinkler before checking the sun.

Take note of the semicolon at the end of the statement. All Java statements require the semicolon, but it may look a little out of place here. Other loops, such as the for loop, don't end with a semicolon. Just remember that the condition check is at the end of the loop: by flipping the roles a little bit, we're required to add the semicolon here.

Here's an example that converts this scenario to Java. Of course, there would be a great deal of other code to determine the sun's position, but let's keep it simple for now!

do {
  sprinkler++
  if(sunVisible == false) {
   sundown = true;
  }
} while (sundown = false);

This code begins a Do-While loop, incrementing the sprinkler run by 1 (sprinkler++). If the sun isn't visible after that run, it sets a flag to false. It won't stop processing until it hits the final while statement. If the sun is down at that point, then processing stops.

Next, let's take a look at some real-world examples.

Examples

There are two wonderful examples of the Do-While loop: A menu and finding a file.

Menu

A great use for the Do-While loop is the menu: The menu is displayed continuously until the end user picks a value. If they type a certain menu item, then the code stops. While the example below may look complicated, pay close attention to the beginning and end of the code: Remember the condition is checked at the end! This ensures the full menu will always be there until the user types a 3.

do {
  System.out.println("***** MAIN MENU *****");
  System.out.println("1. Option 1");
  System.out.println("2. Option 2");
  System.out.println("3. Exit!");
  System.out.println("\n\nSelect an Option: ");
  userOption = keyboard.nextInt();
  switch(userOption) {
   case 1:
    System.out.println("Option1!");
    break;
   case 2:
    System.out.println("Option 2!");
    break;
   case 3:
    break;
  }
} while(userOption != 3);

To unlock this lesson you must be a Study.com 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 Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
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? Study.com 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
Support