Flow charts
Flow charts are a convenient and often used tool for representing the behavior of a program (or portion thereof) in a diagram. They can help us reason about the behavior of a program before we start writing it. If we have a good flow chart, this can be a useful “blueprint” for a program.
We’ll begin with the basics. The most commonly used elements of a flow chart are:
- ellipses, which indicate the start or end of a program’s execution,
- rectangles, which indicate performing some calculation or task,
- diamonds, which indicate a decision, and
- directed edges (a.k.a. arrows), which indicate process flow.
Diamonds (decisions) have one input and two outputs. It is at these points that we test some condition. We refer to this as branching—our path through or flow chart can follow one of two branches. If the condition is true, we take one branch. If the condition is false, we take the other branch.
A minimal example
Here’s a minimal example—a program which prompts a user for an integer, n
, and then, if n
is even, the program prints “n is even”, otherwise the program prints “n is odd!”
In order to determine whether n
is even or odd, we’ll perform a simple test: We’ll calculate the remainder with respect to modulus two and compare this value to zero.1 If the comparison yields True
then we know the remainder when dividing by two is zero and thus, n
must be even. If the comparison yields False
then we know the remainder is one and thus, n
must be odd. (This assumes, of course, that the user has entered a valid integer.)
Here’s what the flow chart for this program looks like:
- We start at the top (the ellipse labeled “start”).
- From there, we proceed to the next step: prompting the user for an integer,
n
. - Then we test to see if the remainder when we divide
n
by two equals zero.- If it does, we follow the left branch, and we print “n is even!”
- Otherwise, we follow the right branch, and we print “n is odd!”
- Finally, our program ends.
Here it is, in Python:
"""
CS 1210
Even or odd?
"""
= int(input('Please enter an integer: '))
n
if n % 2 == 0:
print('n is even!')
else:
print('n is odd!')
The branching takes place here:
if n % 2 == 0:
print('n is even!')
else:
print('n is odd!')
Notice there are two branches:
- the
if
clause—the portion that’s executed if the expressionn % 2 == 0
evaluates toTrue
; and - the
else
clause—the portion that’s executed if the expressionn % 2 == 0
evaluates toFalse
.
Another example: Is a number positive, negative, or zero?
Let’s say we want to decide if a number is positive, negative, or zero. In this instance, there are three possibilities. How do we do this with comparisons that only yield True
or False
? The answer is: with more than one comparison!
First we’ll check to see if the number is greater than zero. If it is, it’s positive.
But what if it is not greater than zero? Well, in that case, the number could be negative or it could be zero. There are no other possibilities. Why? Because we’ve already ruled out the possibility of the number being positive (by the previous comparison).
Here’s a flow chart:
As before, we start at the ellipse labeled “start.” Then we prompt the user for a float, x
. Then we reach the first decision point: Is x
greater than zero? If it is, we know x
is positive, we follow the left branch, we print “x is positive”, and we’re done.
If x
is not positive, we follow the right branch. This portion of the flow chart is only executed if the first test yields False
(that is, x
is not greater than zero). Here, we’re faced with another choice: Is x
less than zero? If it is, we know x
is negative, we follow the left branch (from our second decision point), we print “x is negative”, and we’re done.
There’s one last branch: the one we’d follow if x
is neither positive nor negative—so it must be zero. If we follow this branch, we print “x is zero”, and we’re done.
Here it is in Python,
"""
CS 1210
Positive, negative, or zero?
Using nested if.
"""
= float(input('Please enter an real number: '))
x
if x > 0:
print('x is positive!')
else:
if x < 0:
print('x is negative!')
else:
print('x is zero!')
This structure, which is quite common, is called a “nested if” statement.
Python provides us with another, equivalent way to handle this. We can implement the flow chart for our three-way decision using elif
, thus:
"""
CS 1210
Positive, negative, or zero?
Using elif.
"""
= float(input('Please enter an real number: '))
x
if x > 0:
print('x is positive!')
elif x < 0:
print('x is negative!')
else:
print('x is zero!')
Both programs—the one with the nested if
and the one with elif
—correctly implement the program described by our flow chart. In some cases, the choice is largely a matter of taste. In other cases, we may have reason to prefer one or the other. All things being equal (in terms of behavior), I think the elif
solution presented here is the more elegant of the two.
In any event, I hope you see that flow charts are a useful tool for diagramming the desired behavior of a program. With a good flow chart, implementation can be made easier. You should feel free to draw flow charts of programs before you write your code. You may find that this helps clarify your thinking and provides a plan of attack.
Copyright © 2023–2025 Clayton Cafiero
No generative AI was used in producing this material. This was written the old-fashioned way.
Footnotes
Remember, if we have some integer n, then it must be the case that either n \equiv 0 \mod{2} or n \equiv 1 \mod{2}. Those are the only possibilities.↩︎