Do-While Loop Flowchart
Do-while loop flowchart: the loop body runs first, then a decision at the bottom tests the condition and sends the flow back up while it is true. With a trace table for n = 3, pseudocode, and tested Python, C and Java code.
Do-while loop flowchart: the loop body runs first, then a decision at the bottom tests the condition and sends the flow back up while it is true. With a trace table for n = 3, pseudocode, and tested Python, C and Java code.
Study guide
A do-while loop runs its body first and tests its condition afterwards, so the body always runs at least once. In a flowchart that means the decision diamond sits below the loop body, and its Yes arrow goes back up to the first step of the body.
The example on this page counts down from n: it prints n, subtracts 1, and repeats while n > 0.
n. Reading a value is input, so on paper this step is a parallelogram.n. Notice that nothing has been tested yet: the flow reaches the body without passing a decision.n one smaller. This is the update step. Without it the condition would never change and the loop would never stop.Follow the flowchart with n = 3. Each row is one pass through the loop body, ending at the decision.
| Pass | n at start of pass | Printed | n after n = n - 1 | n > 0? | Next step |
|---|---|---|---|---|---|
| 1 | 3 | 3 | 2 | Yes | Back to Print n |
| 2 | 2 | 2 | 1 | Yes | Back to Print n |
| 3 | 1 | 1 | 0 | No | Print "Done", then End |
Three passes, three tests. The output is 3, 2, 1 and then Done.
READ n
DO
PRINT n
n = n - 1
WHILE n > 0
PRINT "Done"
n = int(input())
# Python has no do-while statement.
# Loop forever, then leave when the condition is false.
while True:
print(n)
n = n - 1
if not (n > 0):
break
print("Done")
#include <stdio.h>
int main(void) {
int n;
scanf("%d", &n);
do {
printf("%d\n", n);
n = n - 1;
} while (n > 0); /* the semicolon is required */
printf("Done\n");
return 0;
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
do {
System.out.println(n);
n = n - 1;
} while (n > 0); // the semicolon is required
System.out.println("Done");
}
}
3
3
2
1
Done
All three programs were compiled and run with the input 3 before this page was published. The output shown is exactly what they printed, and it matches the trace table. Python has no do-while statement, so the Python version uses while True with a break at the bottom of the body, which follows the same flowchart.
n = 0 this flowchart still prints 0 before it tests n > 0. Many exam answers say that nothing is printed.while (n > 0);. A while loop has no semicolon there, a do-while loop must have one.It prints 0 and then Done.
The body runs before any test, so Print n shows 0 and then n becomes -1. The decision asks whether -1 is greater than 0. The answer is No, so the loop ends after one pass.
A while loop with the same condition would print only Done, because it would test 0 > 0 first.
The body runs 5 times and the condition is tested 5 times.
In a do-while loop each pass ends with exactly one test, so the two counts are always equal. The tests are 4 > 0, 3 > 0, 2 > 0, 1 > 0 (all Yes) and 0 > 0 (No).
A while loop that prints the same five numbers would test its condition 6 times: five times Yes and one final No.
READ n
WHILE n > 0
PRINT n
n = n - 1
ENDWHILE
PRINT "Done"
In the flowchart the diamond n > 0? moves up, between Read n and Print n. Its No arrow goes straight to Print "Done". The arrow that leaves Set n = n - 1 goes back up to the diamond instead of down into it.
| Symbol | Shape to draw on paper | Where it appears here | Why |
|---|---|---|---|
| Terminator | Rounded box | Start and End | A flowchart has one entry point and, here, one exit. |
| Input/output | Parallelogram | Read n, Print n and Print "Done" | These steps move data in or out of the program. They are the steps marked data in the diagram. |
| Process | Rectangle | Set n = n - 1 | It changes a stored value and has exactly one way out. |
| Decision | Diamond | n > 0? | A yes-or-no question with two labelled exits. |
| Loop | A decision whose flow line returns to an earlier step | The Yes arrow from n > 0? back up to Print n | A flow line that returns to an earlier step is what makes a loop. Because the diamond is below the body, this is a post-test loop. |
| Flow line | Arrow | Every arrow | Arrows show the order of the steps. Only the loop arrow points upward. |
In the interactive diagram every step is a labelled card: the small word above each step names its kind (process, decision or data). When you draw this flowchart by hand, use the standard shapes in the table. The full list is on the flowchart symbols page.
Each link opens the editor with a prompt already filled in. Change the wording, then generate your own flowchart.
Download the Do-While Loop Flowchart as a PNG or SVG, or open it in the studio to edit your own copy. To draw a similar flowchart from a description, use the flowchart generator; for notation and a step-by-step walkthrough, read how to make a flowchart; and browse more flowchart examples for related layouts.
Type any system, process, or schema in plain English. ChartCraft will draw it in seconds.