C do while Loop

Summary: in this tutorial, you will learn about the C do while loop statement to run a block of code repeatedly based on a condition that is checked at the end of each iteration.

Introduction to the do while loop statement

To run a block of code repeatedly in a predetermined time, you use the for loop statement. In cases you want to run a block of code repeatedly based on a given condition with a check at the end of each iteration, you use the do while loop statement.

The following illustrates the syntax of the do while loop statement:

The do while loop statement consists of execute statements and a Boolean condition. First the execute statements are executed, and then the condition is checked. If the condition evaluates to true, the execute statements are executed again until the condition evaluates to false.

Unlike the while loop, the execute statements inside the do while loop execute at least one time because the condition is checked at the end of each iteration.

The flow chart of the C do while loop is as follows:

c do while loop

C do while loop example

The following example illustrates how to use the do while loop statement:

The program displays exactly 5 times as indicated in do while loop body

In this tutorial, you have learned how to use C do while loop statement to execute a block of code repeatedly with the condition is checked at the end of each iteration.