Posted By:
3 Mar, 2014 1:47 pm PST

The Do-while loop in JAVA

What is do-while loop and how to write it in Java

»All the loop we did above like for and while loop might execute many times,but it is also possible that the loop will not execute at all.

 

»The while loop checks a value at the “top” of the loop before the body has a chance to execute.

 

»The do-while loop checks the value of the loop control variable at the bottm of the loop after one repetition has occurred.It means do-while loop execute atleast once.

 

Syntax:

do {    

    statement

} while (expression);

 

Example:

//To count how many times do-while loop is executed

class DoWhileExample {   

public static void main(String[] args){       

int count = 1;       

   do {           

      System.out.println("Count is: " + count);           

       count++;       

   }while (count < 6);   

}

}

 

Output of the above program is:

Count is : 1

Count is : 2

Count is : 3

Count is : 4

Count is : 5




Please login to reply to this problem.