do-while loop

do-while loop:

In do-while loop body of loop is executed first then only test expression is checked. That’s why the body of do…while loop is executed at least once.

Sytax:

do
{
Block of statement
}
While(condition)

Flow chart:

do while

 

Example 1
#include<iostream.h>

void main()

{

float num, sum1 = 0.0;

do

{

cout<<“Enter a number: “;

cin>>num; sum += num;

}

while(num != 0.0);

cout<<“Total sum = “<<sum1;

}

Output

Enter a number:4.6

Enter a number:6.8

Enter a number:7.1

Enter a number:3.4

Enter a number:6.8

Enter a number:2.0

Enter a number:0

Total sum=30.700001

Example 2

#include <iostream.h>

#include<conio.h>

void main()

{ // Variable Declaration

int a1; // Get Input Value

cout<<“Enter the Number :”;

cin>>a1;

int counterl = 1; //Do while Loop Block

do

{

cout<<“Execute Do While “<<counterl<<” time”<<endl;

counterl++;

}

while (counterl <= a1); // Wait For Output Screen

getch();

}

Output

Enter the Number :2

Execute Do While 1 time

Execute Do While 2 time

Leave a Reply

Your email address will not be published. Required fields are marked *