Unit 4: Algorithms · Building algorithms

Article

Reading and drawing flowcharts

8 min read

A road map shows where the road splits and where it loops back. A flowchart is a map of an algorithm. You follow the arrows like roads, and you see every choice and every repeat at a glance. You will learn to read one, turn it into code, and draw one.

The four parts of a flowchart

PartShapeWhat it means
Start and StopRounded pillWhere the algorithm begins and where it ends
StepRectangleOne instruction, such as setting a variable or displaying a value
DecisionDiamondA question that is true or false. Exactly two arrows leave it, one labelled true and one labelled false
ArrowLine with a headWhich part comes next

To read a flowchart, put a finger on Start and follow the arrows. At a rectangle, do the step. At a diamond, work out the question and take the arrow with the matching label. Stop when you reach Stop. Here is the simplest kind, with steps one after another:

Double a number

Startn ← 6n ← n * 2DISPLAY(n)Stop

n becomes 6, then 12, and the chart displays 12. A line of boxes with no diamond is sequencing, exactly like lines of code from top to bottom.

A decision is selection

A diamond is a choice, so it is selection. This flowchart tells you whether a number is even:

Even or odd

truefalseStartn ← 7n MOD 2 = 0?DISPLAY("even")DISPLAY("odd")Stop

7 MOD 2 is 1, so n MOD 2 = 0 is false. The false arrow leads to DISPLAY("odd"), so the chart displays odd. The two paths join again before Stop. As code, the diamond became IF, the true path became the first block and the false path became ELSE.

AP pseudocode
n ← 7
IF(n MOD 2 = 0)
{
    DISPLAY("even")
}
ELSE
{
    DISPLAY("odd")
}
Runs in your browser

Sometimes one arrow from the diamond leads to a step and the other skips it. That is an IF with no ELSE:

Hot weather note

truefalseStarttemp ← 35temp > 30?DISPLAY("Hot")DISPLAY("Done")Stop
AP pseudocode
temp ← 35
IF(temp > 30)
{
    DISPLAY("Hot")
}
DISPLAY("Done")

With temp at 35 the chart displays Hot Done. With temp at 20, the false arrow skips the Hot step and it displays only Done.

Check your understanding

What is displayed when this flowchart runs?

truefalseStartmark ← 48mark ≥ 50?DISPLAY("Pass")DISPLAY("Retry")Stop

A loop is an arrow that goes back

An arrow that goes back up to an earlier part of the chart makes a loop, which is iteration. A diamond decides when to leave it.

Display 1, 2, 3

truefalseStartcount ← 1count > 3?StopDISPLAY(count)count ← count + 1

Trace it as you trace a loop in code, one row each time you reach the diamond:

Visit to the diamondcountcount > 3?Displayedcount after the loop body
11false12
22false23
33false34
44true

The chart displays 1 2 3. This is a REPEAT UNTIL: the loop leaves when the question is true. The code below matches it step for step:

AP pseudocode
count ← 1
REPEAT UNTIL(count > 3)
{
    DISPLAY(count)
    count ← count + 1
}
Runs in your browser

Python's while runs while its condition is true, so the Python condition is the opposite of the diamond's: count <= 3. Some charts ask the question the other way, count ≤ 3?, and send true into the loop. Do not trust the shape of the question. Read the labels on the arrows: the arrow that goes into the body is the "keep going" arrow.

Check your understanding

Which code segment does the same job as this flowchart?

truefalseStartn ← 1n > 3?StopDISPLAY(n * 2)n ← n + 1

Segment P:

AP pseudocode
n ← 1
REPEAT UNTIL(n > 3)
{
    DISPLAY(n * 2)
    n ← n + 1
}

Segment Q:

AP pseudocode
n ← 1
REPEAT UNTIL(n > 3)
{
    n ← n + 1
    DISPLAY(n * 2)
}

From code to flowchart, and back

Use this table to translate in either direction:

In codeIn the flowchart
Statements one under anotherRectangles in a line
IF with ELSEA diamond, two paths that join again afterwards
IF with no ELSEA diamond, one path with a step and one path that skips it
REPEAT UNTIL(c)A diamond for c: true leaves, false goes into the body, and the last body arrow goes back to the diamond
DISPLAY and assignmentsRectangles

To draw a flowchart from code: (1) draw Start; (2) draw one rectangle for each statement, in order; (3) draw a diamond for each condition and label both of its arrows; (4) for a loop, draw the body and an arrow from its last step back to the diamond; (5) finish with Stop. Try this one on paper:

AP pseudocode
total ← 0
n ← 1
REPEAT UNTIL(n > 3)
{
    total ← total + n
    n ← n + 1
}
DISPLAY(total)

Add 1 + 2 + 3

truefalseStarttotal ← 0n ← 1n > 3?DISPLAY(total)Stoptotal ← total + nn ← n + 1

The body (total ← total + n, then n ← n + 1) sits under the diamond, and the arrow from its last box returns to the diamond. The statement after the loop, DISPLAY(total), is on the true path. Trace it: total goes 0, 1, 3, 6 while n goes 1, 2, 3, 4. At n = 4 the question is true, so the chart displays 6.

Common mistakes

  • Ignoring the labels. Always take the arrow whose label (true or false) matches the question.
  • Reading a diamond as a step. A diamond asks a question. It does not change any variable.
  • Thinking true always means "keep looping". In a REPEAT UNTIL chart, true leaves the loop.
  • Skipping the starting boxes. The values set before the loop are part of the trace.
  • Forgetting that paths join. After an IF, both paths carry on to the same next step.

On the exam

Some questions show an algorithm as a flowchart and ask what it displays, or which code does the same job. Trace it with a table, then check each option against the table. Watch the loop condition and the order of the boxes in the loop body.

Key words

Flowchart: a diagram that shows the steps of an algorithm as shapes joined by arrows.

Decision: a diamond that asks a true or false question and has one arrow out for each answer.

Loop: an arrow that goes back to an earlier part of the chart, so its steps repeat.

Start and Stop: the rounded shapes that mark the beginning and the end.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.