C – Do While Loop

do-while: Executes a block at least once and continue iteration until condition is false.

Syntax:

do
{
            statements;
} while(condition);

Flow Chart :

Check Even numbers until user exits:

#include<stdio.h>
#include<conio.h>
int main()
{
            int n;
            char ch;
            do{
                        printf(“\nEnter number : “);
                        scanf(“%d”, &n);
                       
                        if(n%2==0)
                                    printf(“%d is even \n”, n);
                        else     
                                    printf(“%d is odd \n”, n);
                       
                        printf(“Do you want to check another num(y/n) : “);
                        ch = getch();
                                   
            }while(ch!=’n’);
            return 0;          
}

getch():

  • getch() is a pre-defined method belongs to conio.h header file.
  • It is used to read character from the console while program is running.
Scroll to Top