In this construct, the statements in the loop are executed and the testing is done at the end of the loop. The general form of the statement is
do
{
statements;
}
while (logical expression);
If the expression becomes false, the loop terminates or else the loop continues. This statement is ideal for applications where the code may or may not be repeated but must be performed atleast once.
Consider the same example of printing the numbers from I through 10. The flowchart given below, explains how the do-while construct operates
Example 2.7
In the example given below, even if the value of the variable(number) is value greater initialized to I1 (a value greater than 10), the program will print the value i.e., the loop is executed atleast once. In this manner, the working of do-while construct differs from that of the while-construct.
#include <stdio.h>
int main()
{
int number = 1;
do
{
printf ( "%d",number );
number = number + 1;
}
while (number <= 10);
}