How to use the do...while loop? - ICSE Java Expert

Breaking

ICSE Java Expert

Learn Java for school

Unordered List

WELCOME

Monday, August 06, 2018

How to use the do...while loop?




The syntax for using the do...while loop is given below:

The do...while loop is used when there is requirement of loop that varies which means that it is not known in advance that how many times the loop will executed. Hence it will be decided by the user that whether the loop will iterate for the next cycle or not.

syntax:
do{
/*
* body of the loop
*
*/
}while(condition);

example:
int i=1;
do{
        System.out.println("Hello");
         i++; //  this code can be written as i=i+1;
}while(i<=5);
The above code will print the Hello message for 5 times.



No comments:

Post a Comment