Java while-do and do-while loop syntax

The while loop in Java allows a loop block to be executed an arbitrary number of times until a condition is met. This tech-recipe shows the basic syntax of the Java while loop.

The while loop in Java can be used in two ways, while-do and do-while. The while-do form allows the condition to be tested before the code block is executed, so it is possible for the code block never to be executed. The do-while syntax, like the repeat-until syntax of other languages, does not check the condition until after the loop code block is executed. Thus, the code block will always be run at least once with do-while.

The generic while-do syntax is as follows:

while (condition)

While we refer to this flavor of while loop as “while-do,” it is important to note that the keyword “do” is not used. The do-while syntax does use the “do” keyword:

do {
} while (condition);

The condition can be any valid Java expression that evaluates to a boolean value. The code block can be any valid Java code. If only one line of Java code is required in the code block, the curly braces are optional. However, many programmers leave them in to make their code more readable.

The Conversation

Follow the reactions below and share your own thoughts.

19 Responses to “Java while-do and do-while loop syntax”

  1. November 14, 2008 at 3:34 am, mike said:

    I need a java program that will repeatedly read temperatures in Fahrenheit and convert them to Celsius until a temperature > 212 F is entered. I really need this, please, and thank you. address)

    Here is my java program:
    import java.util.*;
    public class Converter
    public static void main(String[] args)
    Scanner sc = new Scanner(;
    System.out.print(“Enter temp: “);
    int temp = sc.nextInt();
    public static int convertToC(int temp)
    return ((temp – 32 ) * 5/9);


  2. December 10, 2008 at 7:36 am, catherine said:

    can you help me?i need any program on java using do while loop statement..i badly need it here


    • May 24, 2012 at 9:31 pm, valentino said:

      >here is a simple example;

      public class DoWhileLoopDemo{
      public static void main(String []args){
      int i=0;
      System.out.println(“I love java”);
      i++;//incremented on the do body
      }//ends do body


  3. April 13, 2009 at 6:23 pm, waseemwaheed said:

    it is best site


  4. April 13, 2009 at 6:25 pm, waseemwaheed said:

    please send me best language programs at my e-mail.


  5. May 20, 2009 at 7:17 am, vhel said:

    how can i create a program like this…
    pls help me
    Design and implement a program using while and for loop that will prompt the user to enter a number. The program will give all numbers that divide that number.
    Please enter a number: 36
    Numbers dividing 36 are: 1 2 3 4 6 9 12 18 36


  6. June 08, 2009 at 4:02 pm, hari krishna said:

    do while…. where it will be used.


    • September 10, 2010 at 12:02 am, Truelyunique360 said:

      It will be used when you know that the loop will be executed at least once. Hop that helps.


  7. August 08, 2009 at 2:07 am, Anonymous said:

    hi hoe you can help me in having a code of asp


    • July 19, 2010 at 9:19 am, RAJKANNAN said:

      i want the using class print the student Details


  8. September 28, 2010 at 6:47 am, Jewelmartinez08 said:

    can you please give me a sample unique java program please……..


  9. September 28, 2010 at 6:50 am, Jewelmartinez08 said:

    here is my e-mail add thanks


  10. January 31, 2011 at 1:47 pm, Nardaxmcs said:

    can you help me? make a program with this problems:

    1. compute the evaluation rating of a teacher given 5 students. display average..


    2. display the highest number from 10 input integers.

    thanks.. please send it to my e-mail,



  11. February 24, 2011 at 7:49 am, Nickbhel said:

    how can i make a program that determine prime#’s


  12. July 19, 2011 at 2:02 pm, claire fatima inodeo said:

        public static void main(String[] args, boolean flag) {
            int x = 1;
            int y = 3;
                       while (x


  13. February 26, 2012 at 7:56 pm, allison said:

    java is sooooooooooooooo coool!!!!!


  14. June 26, 2012 at 11:31 am, Maduhara said:

    who is know create stars in cmd using for loop jst like this

    class NESTED {
    public static void main(string arg[]){
    int i,j;


  15. June 26, 2012 at 11:38 am, Maduhara said:

    bt this is nnt working ma script


  16. June 30, 2012 at 2:27 pm, Nouswey wiliam said:

    I want help on how to write a java program that will print numbers or letters in reverse order


Leave a Reply