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)
{
code_block;
}

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

  • mike

    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. mbnumba14@yahoo.com(email address)

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

  • catherine

    can you help me?i need any program on java using do while loop statement..i badly need it..email it here babytsk27_iilamagawaii_mac518@yahoo.com..thanks.

    • valentino

      >here is a simple example;

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

  • http://www.tech-recipes.com/rx/673/java-while-do-and-do-while-loop-syntax/ waseemwaheed

    it is best site

  • http://www.tech-recipes.com/rx/673/java-while-do-and-do-while-loop-syntax/ waseemwaheed

    please send me best language programs at my e-mail. waseemwaheed_31@yahoo.com

  • vhel

    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.
    Example:
    Please enter a number: 36
    Numbers dividing 36 are: 1 2 3 4 6 9 12 18 36

  • hari krishna

    do while…. where it will be used.

    • Truelyunique360

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

  • Anonymous

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

    • RAJKANNAN

      i want the using class print the student Details

  • Jewelmartinez08

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

  • Jewelmartinez08

    here is my e-mail add jewelmartinez08@yahoo.com thanks

  • Nardaxmcs

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

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

    and

    2. display the highest number from 10 input integers.

    thanks.. please send it to my e-mail, nardaxmcs@gmail.com..

    thanks…

  • Nickbhel

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

  • http://twitter.com/claire143_13 claire fatima inodeo

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

  • allison

    java is sooooooooooooooo coool!!!!!

  • Maduhara

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

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

  • Maduhara

    bt this is nnt working ma script

  • Nouswey wiliam

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

  • zainab

    help me please \
    what if i want to do a loop that gives me all the numbers that can be decided by 3 like

    numbers can be decided by 3 : 3 , 6 , 9