Coding

For Loop Flowchart: How to Draw and Read One

A for loop flowchart has five parts: an initialisation box that runs once, a condition diamond, the loop body, an update box, and an arrow from the update back to the condition. When the condition is false, a second arrow leaves the loop.

The flowchart below is the standard picture for a counting loop such as for (int i = 0; i < 3; i++). It is text, so you can copy it into a notes file or redraw it in any diagramming tool.

   [ Start ]
       |
       v
 [ i = 0 ]              <- init: runs once
       |
       v
  /-----------\   false
 < is i < 3 ?  > ----------+
  \-----------/             |
       | true               |
       v                    |
 [ loop body ]              |
       |                    |
       v                    |
 [ i = i + 1 ]              |
       |                    |
       +-- back to the      |
           condition        |
                            v
                       [ Next statement ]

How to read a for loop flowchart

Follow the arrows from the top. The init box is visited once and never again, because no arrow points back to it. The only arrow that goes backwards runs from the update box to the condition. That single backward arrow is what makes it a loop.

The order matches what the language specifications describe. MDN states that the initialization runs once before the loop begins, the condition is evaluated before each iteration, and the afterthought is evaluated at the end of each iteration, before the next evaluation of the condition. The C reference says the same thing in other words: the iteration expression runs after the loop body, and control then goes to the condition.

Trace it with real values

Take the loop for (int i = 0; i < 3; i++) with a body that prints i. Walking the flowchart gives this table:

StepBox visitedi after the stepResult
1Init0Runs once
2Condition i < 30true, enter body
3Body0prints 0
4Update1back to condition
5Condition1true
6Body, then update2prints 1
7Condition, body, update3prints 2
8Condition3false, leave the loop

The condition is checked four times, but the body runs three times. The condition always runs one more time than the body, because the last check is the one that fails. Remembering this removes most off-by-one confusion when you read a flowchart.

Where break and continue go

Both statements add an arrow that does not come from the normal path.

  • break draws an arrow from the decision that triggers it straight to the next statement after the loop. The condition diamond is skipped.
  • continue draws an arrow from the triggering decision to the update box. The cppreference page says continue "transfers control to iteration-expression", and MDN says it re-evaluates the afterthought and then the condition. So the counter still advances, and the loop cannot get stuck on the same value.

Here is a loop that skips 2, with the extra decision it adds to the flowchart:

for (int i = 0; i < 5; ++i) {
    if (i == 2) continue;   // arrow goes to ++i, then to i < 5
    printf("%d ", i);
}
/* Example output: 0 1 3 4 */

In the flowchart, a second diamond i == 2 ? sits between the condition and the print box. Its true exit goes to the update box. Its false exit goes to the print box, which then also flows into the update box.

Omitted parts: the infinite loop

All three clauses in a C or JavaScript for header are optional. If the condition is omitted, MDN says it always evaluates to true, and cppreference says it is replaced by a non-zero constant, so for (;;) is an endless loop. In the flowchart, you delete the condition diamond, or leave it with only a true exit. The only way out is an arrow from a break, a return or a program exit inside the body. If you draw an endless loop with no such arrow, you have drawn a program that never ends.

For each loops in a flowchart

Python's for has no counter in its header. The Python reference says the iterable is evaluated once and an iterator is created, and then each item is assigned to the target before the suite runs. The same five boxes still work, with different labels:

  • Init: create an iterator from the iterable (once).
  • Condition: is there another item?
  • Update: assign the next item to the loop variable, then run the body.

Python adds one more exit. When the iterator is exhausted, the else suite runs if there is one, and a break skips it. In a flowchart, that means the false exit of the condition goes to the else box, while the break arrow bypasses it. Python also notes that assigning to the loop variable inside the body does not change the iteration, because the next item overwrites it.

Drawing tips

  1. Label both exits of every diamond with true and false (or yes and no).
  2. Put the update box after the body, not before the condition. Putting it before the condition is the most common mistake, and it makes the first iteration start at the wrong value.
  3. Keep one backward arrow per loop. If you need two, one of them is probably a continue and should come from a decision diamond.
  4. Write the actual expressions in the boxes (i = 0, i < 3) rather than "initialise" and "check". Then anyone can trace it with a pencil.

Related articles

For the code behind the flowchart, see For loop in C: syntax, examples, break and continue or JavaScript for loop: syntax and examples. To flowchart a loop inside a loop, read nested for loops explained: the inner loop is a complete copy of this flowchart sitting inside the outer loop's body box.

Frequently asked questions

What shape is the condition in a for loop flowchart?
The condition is drawn as a decision shape, usually a diamond, with two exits labelled true (or yes) and false (or no). The true exit leads to the loop body. The false exit leaves the loop. Shape conventions vary slightly between teams, so label both exits whatever shape you use.
Where does the update step go in a for loop flowchart?
After the loop body, with an arrow from it back to the condition. In C and JavaScript the update (the afterthought or iteration expression) runs at the end of every iteration, before the condition is checked again. It never sits before the condition on the first pass.
How is a for each loop different in a flowchart?
A for each loop, such as Python's for x in items, has no counter, condition or update of its own. The condition becomes 'is there another item?', the init step becomes 'create an iterator', and the update step becomes 'assign the next item to the loop variable'. The shape of the flowchart stays the same.
Does continue skip the update step?
Not in C or JavaScript. Both languages send continue to the update step, then to the condition, so a counter still advances. In a flowchart, the continue arrow points at the update box, not at the condition. A while loop is different: it has no update step, so continue goes straight to the condition.

Sources