×
☰ Menu

do-while Loop

 

The do-while loop looks like this: 

do 
{ 
Body of loop; 
} while ( this condition is true ) ; 

 

While and do-while loops have a tiny distinction in how they work. This distinction is the location where the condition is assessed. Before executing any of the while loop's statements, the while loop checks the condition. The do-while, on the other hand, evaluates the condition after the statements within the loop have been executed.

Even if the condition fails for the first time, Do-while will execute its statements at least once. If the condition fails for the first time, the while loop will not execute its statements.

Code:

#include<stdio.h>
#include<conio.h>
int main( )
{
int p, n, count ;
float r, si ;
count = 1 ; 
do
{
printf ( "Enter values of p, n, and r: \n" ) ;
scanf ( "%d %d %f", &p, &n, &r ) ;
si = p * n * r / 100 ;
printf ( "Simple Interest = Rs.%f\n", si ) ;
count++ ;
}
while(count <= 3 );
getch();
}

Output:

Enter values of p, n, and r:
1000
50
5
Simple Interest = Rs.2500.000000