Saturday, 28 July 2018

Difference between while statement and do while statement used in C

While statement ::

Syntax :: (1)  while ( condition )
                 statement ;

               (2) while ( condition )
                    {
                     statement 1 :
                        "
                        "
                    }

Do while statement ::

Syntax ::   do 
                 {
                 statement 1 :
                     "
                     "
                     "
                  } while ( condition ) ;

What is difference between while and do while?

As per syntax property , in case of while statement first of condition is checked and and if the condition is true then execution of number of statement written within pair of { } takes place. If the condition is false the execution of number of statement written under the condition of while loop is not possible.
                     On the other hand in case of do while statement first of all execution of number of statement takes place and after executing once the condition is checked further if condition is true then execution of statement within do while is possible otherwise execution of statement is not possible.
              It means in case of while statement the execution of statement or number of statement is always condition dependent . 
                                                In case of do while statement the first execution is always condition independent. it is because of the syntax property of while and do while statement. 

Uses of do while in a program ::

#include <stdio.h>
void main ()
{
int a=1 ;
clrscr () ;
do
{
printf ("%d", a) ;
a++ ;
} while ( a <= 10 ) ;
getch () ;
}

No comments:

Post a Comment

Featured post

Program to find Volume of given box in java

class volume { public static void main (String args []) { int l=5,b=6,h=10; int volume; volume=l*b*h; System.out.println (" vol...