Bcaguru

Statement ‘do-while’  in C

The ‘do-while’ statement is used  4 the  execution of a single statement or block of statements again & again as long as the condition given is TRUE.

 The do-while statement is also known as Exit control looping statement is the another name of do-while.

The syntax of ‘do-while statements are given below :-   {Observe the pictorial syntax of do-while statement}…………….>

                                    Syntax :–

                                    do

                                    {

                                            ………….

                                    Block of statements

                                           …………..

                                    }

                                     while (condition) ;

Execution flow diagram of do-while statements are given below… Observe the diagram fairly:-

At the beginning, the single statement or block of statements which are defined in do block are executed. After the execution of do block, the given condition gets evaluated.

                     If the condition is evaluated to TRUE, the single statement or block of statements of do block are executed again. Once the execution is finished, again the condition will be evaluated.

                     If next condition is also TRUE, the same statements are executed again. The same process is repeated until the condition is evaluated to FALSE, the same process of evaluation of condition will repeat again & again. The execution control moves (jumps) out of the while block whenever the condition is evaluated to FALSE.

Example Program | Write a program to display even numbers upto 10.

#include<stdio.h>

#include<conio.h>

void main(){

   int n = 0;

   clrscr() ;

   printf(” All the even numbers upto 10are\n”);

   do

   {

      if( n%2 == 0)

         printf(“%d\t”, n) ;

      n++ ;

   }while( n <= 10 ) ;

   getch() ;

}

Output:

            All the even numbers upto 10are

            0              2           4             6           8         10

THE MOST IMPORTANT POINTS TO BE REMEMBERED:

When we use do-while statement, we must have to follow the following steps to use do-while statements given below:-

1) do and while both are keywords so they must be used only in lower case letters.

2)If the given condition has variable, it must be assigned a value before it is used.

  • 3)The variable value used in condition must be changed according to the requirement inside the ‘do’ block.
  • 4)The condition can be either a direct integer value or a variable; in do-while statement .
  • 5) A do-while statement can also be an empty or {NULL} statement.(At the execution time, the compiler neglect the empty or NULL statements.)
  • 6) In do-while, the block of statements are executed atleast once.

Leave a Reply

Close Menu