How does the do…while Loop works?

Syntax Structure of do while loop

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.

The flowchart below shows the visual representation of the flow of the do…while loop in C.

do…while Loop in C

Loops in C language are the control flow statements that are used to repeat some part of the code till the given condition is satisfied. The do-while loop is one of the three loop statements in C, the others being while loop and for loop. It is mainly used to traverse arrays, vectors, and other data structures.

Similar Reads

What is 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 to Use do…while Loop in C

The following example demonstrates the use of do…while loop in C programming language....

How does the do…while Loop works?

...

C do…while Loop Flowchart

Syntax Structure of do while loop...

Nested do…while Loop in C

Flowchart of do…while Loop in C...

Examples of do…while Loop in C

As with other loops, we can also nest one do…while loop into another loop. It is demonstrated using the following C program....

Difference between while and do…while Loop in C

...

Conclusion

Example 1. C Program to demonstrate the behavior of do…while loop if the condition is false from the start....

FAQs on C do…while Loops

...