How does do while loop works in C ?

do…while Loop in C

The do…while in C is a loop statement used to repeat some part of the code till the given condition is fulfilled. It is a form of an exit-controlled or post-tested loop where the test condition is checked after executing the body of the loop. Due to this, the statements in the do…while loop will always be executed at least once no matter what the condition is.

Syntax of do…while Loop in C


do {

// body of do-while loop

} while (condition);



How does the do…while Loop works?



Syntax Structure of do while loop


The working of the do…while loop is explained below:

When the program control first comes to the do…while loop, the body of the loop is executed first and then the test condition/expression is checked, unlike other loops where the test condition is checked first. Due to this property, the do…while loop is also called exit controlled or post-tested loop.

When the test condition is evaluated as true, the program control goes to the start of the loop and the body is executed once more.

The above process repeats till the test condition is true.

When the test condition is evaluated as false, the program controls move on to the next statements after the do…while loop.

As with the while loop in C, initialization and updation is not a part of the do…while loop syntax. We have to do that explicitly before and in the loop respectively.



Comments