Dowhile

Do While loops #

A do while loop is similar to a while loop except that it always runs the block of code first, and then at the end if checks to see if it should run the loop again. Do while loops are used when you know that you will always want to run the block of code at least once:

int number;
do
{
    printf("Please enter a nonzero positive value: ");
    scanf("%d", &number);
}
while(number <= 0);

The above code will run the block of code after the do once, and then it will evaluate the condition in the while statement at the end and decide if it should run the block of code again. Like the while loop it is possible to have an infinite loop if you do not modify the variable being tested in the condition inside the block of code.

Note that the do while loop requires a semi-colon at the end as it does not end with {}.