Difference between while and do-while statements in Java

1.  while loop is an entry controlled loop and do-while loop is an exit controlled loop.
2.  while loop first check the condition and then execute statements body .
     do-while loop first execute statements body and then check the condition.
3.  do-while loop execute the statement body at least once but while loop not.
4.  Syntax of while loop : 
                                        while(condition){
                                             // statements
                                          }

     Syntax of do-while loop :
                                         do{
                                                // statements
                                              }while(condition);
    
  
 5. Example of while loop :

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

         Example of do-while loop :

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


Find us :
        Facebook : @apnaandroid
        Google+   : Apna Java
        YouTube : Android & Java Tutorial

Comments

Popular posts from this blog

Disable/Hide Year from DatePickerDialog in android

Custom Calendar in android

Constructor in Java