Loading....
Coupon Accepted Successfully!

 

The do-while Loop

The format of do-while loop in C/C++ is as follows (Figure)

 


do-while loop

 

The difference between the while and do-while loop is the place/position where the condition is to be tested. In the while loops the condition is tested following the while statement and then the body gets executed. Whereas in the do-while the condition is checked at the end of the loop. The do-while loop will execute at least one time even if the condition is false initially. The do-while loop executes until the condition becomes true.
 

Table: shows the comparison between the while and do-while loops.

 

Table: Comparison of while and do-while loop.

Sr No

while-loop

do–while

1

Condition is specified at the top

Condition is mentioned at the bottom

2

Body statement/s is/are executed when condition is satisfied

Body statement/s execute even when condition is false

3

No brackets for a single statement

Brackets are essential even when a single statement exit

4

It is entry controlled loop

It is exit controlled loop


Programs on do-while loop are as follows.


6.9 Write a program using do-while loop to print numbers and their cubes up to 10.


Explanation: Here, the mathematical function pow (x,3) is used from the library. It’s meaning is to calculate the third power of x. With this function we get the value of y=x3. For use of the pow() function we have to include math.h header file.





Test Your Skills Now!
Take a Quiz now
Reviewer Name