Do While Loop

The Do…While loop is a repetition structure where the statements inside the loop are executed at least once. Only then after being executed once, the Boolean expression is evaluated. If the Boolean expression is true, the body of the loop is executed again and the Boolean expression is re-evaluated once again. Note that this is different from the while loop, where the Boolean expression is at the top. Being at the top in a while loop, it is evaluated first and there might be a circumstance where the Boolean expression is false, right from the beginning. In this case the while loop will never happen. In a Do…While loop, the statements will always happen at least once.

Note that not all programming languages have a Do…While loop structure.

The Do…While loop (in most computer programming languages) takes the generic form of:

DO
statement(s)
counter = counter + 1
WHILE (boolean expression)

In this example program once again the user is asked to enter a positive integer and the program will count how many times it goes through the loop until it reaches that number. This time a Do…While loop is used.

Top-Down Design for Do…While loop

Top-Down Design for Do…While loop

Flowchart for Do…While loop

Do…While loop flowchart

Pseudocode for Do…While loop

GET positive_integer
DO
SHOW counter
counter = counter + 1
WHILE (counter < positive_integer)

Code for Do…While loop

Example Output

Code example output