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.
do { // body of do-while loop } while (condition);
How does the do…while Loop works? The working of the do…while loop is explained below: 1. 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. 2. 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. 3. The above process repeats till the test condition is true. 4. 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.
Program.c
output.c
With supporting text below as a natural lead-in to additional content.