Difference between while and do while loop in C

Difference between while and do while loop in C

Difference between while and do while loop

while loop

do while loop

while loop is an entry control loop

do while loop is an exit control loop

In while loop condition is evaluated at the beginning

do while loop, where the condition is evaluated at the end

If the condition is not satisfied then body of the loop will not execute at least one time.

In this loop, the body of the loop will execute for a single time if the condition does not satisfy at the very first attempt.

It consists of keyword while only

It has used keyword do and while

Loop is not terminated with a semicolon

Loop is terminated with a semicolon

Syntax

Initialization

while(condition)

{

        Body of loop;

        Increasing/ Decreasing;

}

 

Syntax

 do

            {

                    Body of loop;

                      Increasing/decreasing;

               }

              while(condition);

 

Flow chart

 


Flow chart

 


Example

 Write a C program to print “C is best” 10 times

 #include<stdio.h>

#include<conio.h>

void main()

{

                int i=1;

                while(i<=10)

                {

                                printf("\n C is best");

                             i++

                }

                getch();

}

 

Example

C program to print "C is best" 10 time using for loop

#include<stdio.h>

#include<conio.h>

main()

{

          int i=1;

    do

          {

                   printf("\n C is best");

                   i++;

          }

          while(i<=10);

          getch();

 

 


Post a Comment

0 Comments