Python Loops and Flowcharts

In this lesson we are going to learn how to convert between Python code and flowchart representations of loops when designing algorithms.

Consider the following code. As with most code examples in these lessons, you will learn a lot more if you actually type (not copy/paste) and run the code on a real system, whether using an actual Python installation on your computer or a browser-based Python interpreter.

count = 1

count = count + 1
count = count + 1
count = count + 1
count = count + 1

It prints the numbers from 1 to 5, by repeatedly modifying the value of the variable count and outputting the result.

The corresponding flowchart representation would be

Python flowchart repetition without a loop

Now computers are very good at repetition, and there is certainly some repetition in the above code and flowchart.

DRY is an important programming principle you should adhere to unless there is good reason not to – “Don’t Repeat Yourself”.

So, programming languages have loops to help avoid unnecessary repetition. You may be aware that there are two main types of loop in Python – while loops and for loops. What you may not be aware of is just how similar these two types of loop are “under the hood”. Basically a for loop is syntactic sugar for a while loop, meaning a useful shorthand for code which does the same thing.

Representing Loops in a Program using a Flowchart

Here is a flowchart showing how the algorithm above can be implemented using a loop. In order to keep track of the repetitions, a variable is introduced called count. This could in principle be called anything. Some common names are i, x, n, idx, counter etc. One key thing to notice is that the way the condition inside the decision symbol (rhombus) is represented is different to how it is generally represented in programming languages. We have count >= 5 ?, which is perfectly logical, but looks different to how it would look after an if or while keyword. You will see this shortly.

Python Loops Flowchart Representation

Implementation of Loops in Python

Below are some Python implementations of the same algorithm in Python. They are all equivalent, which illustrates the fact the there are usually multiple ways to implement an algorithm presented in flowchart form.

This first implementation is the closest to the flowchart in terms of a direct conceptual mapping.

count = 1
while True:
    if count >= 5:
        count = count + 1

It uses while True to create an infinite loop which is only broken out of when the the condition in the if statement is met. If you think about how the flowchart works, hopefully the equivalence will be apparent.

Some people claim that while True loops should never be used in programming. This is a superstition based on a misunderstanding. They are possible in the language for a reason, and often provide the most direct mapping to concepts in Computer Science syllabuses.

If for some reason you feel you need to avoid using a while True loop, an alternative version is:

count = 1
while count <= 5:
    count = count + 1

This is functionally equivalent to the previous version, but notice that it does not provide a direct conceptual mapping, and the exit condition has to be inverted compared to the flowchart.

Next there is the for loop version, which is probably the most convenient way to implement the algorithm from a programmer’s point of view. The connection to the flowchart is harder to discern here though, until you have some experience with these kinds of translations.

for count in range(1, 6):

Notice that the Python range function stops one short of the second arguments, so the above code assigns values from 1 to 5 to count.

Finally for any of you who have the misfortune to be expected to work with something called “pseudocode” rather than a lovely clean language like Python which provides all the benefits which “pseudocode” is supposed to provide with none of the major downsides (you may sense a hostility towards pseudocode, and you are right. That is for another article though):

for count = 1 to 5
    print count
next count


FOR count = 1 to 5
    OUTPUT count


FOR count = 1 TO 5
NEXT count


Quite how these are an improvement on Python or any other real language eludes me, but I include them for the sake of completeness. Anyone who learned to code back in the age when home computers first became popular will recognise that these are basically (!) just varying versions of implementations in the BASIC programming language which has long been superseded by more elegant and powerful modern languages.

In this article we have seen how to convert between program statements in Python, and flowchart representations in algorithms requiring repetition. I hope you found it helpful

Happy computing!

Sharing is caring!

Leave a Reply

Your email address will not be published.