The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

'do-while' statement in C

The do-while statement is used to execute a single statement or block of statements repeatedly as long as given the condition is TRUE. The do-while statement is also known as the Exit control looping statement. The do-while statement has the following syntax...

do while statement syntax

The do-while statement has the following execution flow diagram...

do while statement

At first, the single statement or block of statements which are defined in do block are executed. After the execution of the 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 gets completed again the condition is evaluated. If it is TRUE, again the same statements are executed. The same process is repeated until the condition is evaluated to FALSE. Whenever the condition is evaluated to FALSE, the execution control moves out of the while block.

Example Program | Program to display even numbers upto 10.

#include<stdio.h>
#include<conio.h>

void main(){
   int n = 0;
   clrscr() ;
   printf("Even numbers upto 10\n");
   
   do
   {
      if( n%2 == 0)
         printf("%d\t", n) ;
      n++ ;
   }while( n <= 10 ) ;
   
   getch() ;
}

Output:

do while in c programming

MOST IMPORTANT POINTS TO BE REMEMBERED

When we use the do-while statement, we must follow the following...

  • Both do and while are keywords so they must be used only in lower case letters.
  • If the condition contains a variable, it must be assigned a value before it is used.
  • The value of the variable used in the condition must be modified according to the requirement inside the do block.
  • In a do-while statement, the condition may be a direct integer value, a variable or a condition.
  • A do-while statement can be an empty statement.
  • In do-while, the block of statements is executed at least once.

Place your ad here
Place your ad here