The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.
Flow chart of do while.

  • Syntax of do-while loop


    Syntax :

        
    do{  
        //code to be executed  
        }while(condition/expression);  


    Example :

       
     class Example {  
        public static void main(String[] args) {  
            int i=1;  
            do{  
                System.out.println(i);  
            i++;  
            }while(i<=10);  
        }  
        }  

    Output:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
  • Difference between while and do-while loop

    while loop do-while loop
    The while loop is pretested . The do-while loop is postested .
    while loop will execute only if the condition is true The while loop is entry controlled loop. while loop will execute at least once with respect to the condition . The do - while loop is exit controlled loop.
  • Difference between for and do / do-while loop

    for while / do-while
    The loop is used when the number of repetition are known . The loop is used when the number of repetition are known as well as unknown .
Important point

If we want to remove the last digit of a number from the right then we will use :
n= n % 10 ;

What next?

The next topic is Jump Statements in java

Share this page