Welcome to Bucaro TecHelp!

Bucaro TecHelp
HTTPS Encryption not required because no account numbers or
personal information is ever requested or accepted by this site

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Java do-while Loop

A loop is a block of statements that are repeated. A loop may be repeated a fixed number of times, repeat until some condition is met, or can loop indefinitely while the program is running. There are three main loop control structures available in Java: for, do and while. The do-while loop is similar to the while loop, except the conditional test is performed after the execution of the code in the loops code block. Shown below is an example of a do-while loop.

int iVal = 0;

do {
} while(iVal < 10);

The do-while loop starts out by executing the code inside the code block of the loop. Inside the code block of the while loop, the value of iVal is incremented (increased) by 1. After the code inside the code block is executed, the conditional test checks if the value in the variable iVal is less than 10. While this condition is true, the do-while loop continues. The value of iVal will be incremented each time the while condition is true. On the tenth iteration through the loop, the value of iVal will not be less than 10, so the condition in the while text will turn false and the loop will exit.

iValcode blockresult
0inc iVal to 1true
1inc iVal to 2true
2inc iVal to 3true
3inc iVal to 4true
4inc iVal to 5true
5inc iVal to 6true
6inc iVal to 7true
7inc iVal to 8true
8inc iVal to 9true
9inc iVal to 10false > exit loop

Note: some important things about the do-while loop is that the statements within the do-while code block are always executed at least once, and something must happen within the code block to change the test variable or it will loop indefinitely.

So, the resulting function of this while loop example is to generate the numbers 1 through 10. Shown below is the complete code for an application that displays the output in a program window.

import javax.swing.*;

public class doWhileLoop 
   public static void main(String s[])
      JFrame frame = new JFrame();
      frame.setTitle("Java do While Loop Test");

      String outData = "";
      int iVal = 0;

      do {
         String strI = String.valueOf(iVal);
         outData += strI + " ";
      } while(iVal < 10);

      JTextArea myText = new JTextArea();


In the code above, the code in the do-while loop code block uses the valueOf method to convert iVal to a string and then concatenates it onto the end of the string variable outData. A JTextArea object named myTextis created, and the line myText.append(outData) places the outData string in the JTextArea for display in the program window.

Output of do-while loop example

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro


Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2024 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268