# Flow Chart For a 'for' Loop In Python

## Main Question or Discussion Point

I am currently learning python and to understand the my code fully and to make notes I am trying to draw flow charts to compliment my code. My issue is I am not sure what to put in the flow charts or really how to represent them as clearly as possible. I have done a google search but I am bit confused as there seem to be :

1. More that one way to represent a simply for loop flow wise

2. Seem language dependent i.e python flow chart is different to C flow chart for example

My current attempt

What I would like to know is there a general way to do flow charts for or is it up to the person who designs the program? I understand the the shape of the boxes represent diffrent things i.e the top box, is speciffically for 'start/end' and the dimond box is for 'decisions' ect but I more wondering about how you draw them and what type of information you put in each box.

#### Attachments

• 12.4 KB Views: 3,957

Related Programming and Computer Science News on Phys.org
anorlunda
Staff Emeritus
What I would like to know is there a general way to do flow charts for or is it up to the person who designs the program?
I think you can compare it to the rules for writing a sentence. Yes there are rules, but there is also much freedom of expression.

One rectangular box in a flow chart could represent a micro action (e.g. clear the A register) or a macro action (e.g. prepare and file your taxes).

Klystron, .Scott and Taylor_1989
jedishrfu
Mentor
FactChecker and Taylor_1989
A little research will show you possibly more than you're looking for -- here's a modified version of your chart to show a little more than you had, but a little less than you're looking for -- it's a flowchart for a FOR-NEXT loop in BASIC (in Python the structure could be similar) that prints the first 10 positive integers:

Last edited:
Klystron, jedishrfu and Taylor_1989
@Taylor_1989 9 (and for others who may be reading) There's a detail missing in the modified version of the flowchart. It doesn't show what happens to the value of ##\mathtt{I}## at the end of the loop.

1. Should the Stop Loop box adjust the value? (hint: if it doesn't, as the flowchart stands, the value will be 11 after the loop ends).

2. If you wanted the value to be 10 after the loop, you could change the symbol in the test in the diamond from ##>## to ##=##, but that would meaning printing only up to 9. To fix that, you could put the Print ##\mathtt I## box ahead of the diamond, but you'd have to adjust some of the flow arrows. Can you draw the new chart that way, or some other way, or describe the changes that would have to be made?

I'll post an answer to those questions a little later.

jedishrfu
jedishrfu
Mentor
There are always side effects and edge cases to consider when programming.

You might say programmers really live life to the fullest on the edge.

Klystron
Ibix
Doesn't python do for loops by iterating over a list? In which case the flowchart needs to show the list, initialise a pointer to the head of the list, update the pointer, and check for reaching the end of the list.

anorlunda
Staff Emeritus
It doesn't show what happens to the value of ##I## at the end of the loop.
Ouch, that's bad practice. You might get different answers in different languages or dialects. Much better to presume that I exists only within the loop and is undefined outside the scope of the loop.

Klystron and FactChecker
Ouch, that's bad practice. You might get different answers in different languages or dialects. Much better to presume that I exists only within the loop and is undefined outside the scope of the loop.
Shouldn't the flowchart reflect that detail?

Doesn't python do for loops by iterating over a list? In which case the flowchart needs to show the list, initialise a pointer to the head of the list, update the pointer, and check for reaching the end of the list.
The reason for the modification being in BASIC was to allow some guidance without depriving @Taylor_1989 of the benefit and satisfaction of completing the python-specific but otherwise general case himself.

Python For loops behave differently from their BASIC counterparts. You can check what Python For loops can iterate over, including lists, here: https://wiki.python.org/moin/ForLoop

Last edited:
symbolipoint
Homework Helper
Gold Member
I only know some of BASIC, but no other programming languages (SOME of; not most of it).

In using a FOR-loop, the program will know or be given the number of times to run through the loop. As example, you have a program with a number c, for how many runs through the FOR-loop. Run through the loop body c number of times and then this loop session will be completed. (I am not making the drawing but the code may be like this:)

Code:
FOR i=1 to c
some line of code
some further lines of code maybe
NEXT i
If a specific example would help, this FOR-loop finds and shows the first eight multiples of 3:
Code:
LET c=8
REM value of variable c could be calculated somewhere earlier in the program.

FOR i = 1 to c
p=3*c
PRINT p
NEXT i
That's what they can look like in BASIC. I do not know about Python.

EDIT: Attempting to include picture but only see choice, "Attach files". Then there's button "Thumbnail Full image".

Last edited:
There are always side effects and edge cases to consider when programming.

You might say programmers really live life to the fullest on the edge.
You're no doubt aware that the level of detail for a flowchart should be determined by the intended use. Some old-school examples can be found in this manual: IBM System/360 Operating System Assembler (32K) Program Logic Manual

A lot of ingenuity went into squeezing that much functionality into 32K.

Klystron
I only know some of BASIC, but no other programming languages (SOME of; not most of it).

In using a FOR-loop, the program will know or be given the number of times to run through the loop. As example, you have a program with a number c, for how many runs through the FOR-loop. Run through the loop body c number of times and then this loop session will be completed. (I am not making the drawing but the code may be like this:)

Code:
FOR i=1 to c
some line of code
some further lines of code maybe
NEXT i
If a specific example would help, this FOR-loop finds and shows the first eight multiples of 3:
Code:
LET c=8
REM value of variable c could be calculated somewhere earlier in the program.

FOR i = 1 to c
p=3*c
PRINT p
NEXT i
That's what they can look like in BASIC. I do not know about Python.
Do you know what the value of the counter variable will be after the loop has completed?

symbolipoint
Homework Helper
Gold Member
Do you know what the value of the counter variable will be after the loop has completed?
No, I am unsure. I could guess, but some other member undoubtedly can give a more reliable answer. I do not know if the variable is reset after finishing loop or if it keeps its last incremented value. You could try putting a PRINT i statement AFTER the loop and see what the statement tells you.

symbolipoint
Homework Helper
Gold Member
I wish I could edit my post #13 in case I get time to make a drawing of a flow-diagram and post it into that post - otherwise I may just make an additional post.
My mistake - I was looking at someone else's post. Mine does have an "edit", for me to choose.

Last edited:
No, I am unsure. I could guess, but some other member undoubtedly can give a more reliable answer. I do not know if the variable is reset after finishing loop or if it keeps its last incremented value. You could try putting a PRINT i statement AFTER the loop and see what the statement tells you.
In Basic it's set to 0. You're right about how to prove that.

QuantumQuest
Gold Member
What I would like to know is there a general way to do flow charts for or is it up to the person who designs the program?
A flowchart is a graphical representation of an algorithm or of a portion of an algorithm. Like pseudocode, flowcharts are used to help in the development and in the representation of algorithms, although most programmers prefer pseudocode. So, these are enough to show the generality of a flow chart as a mean of graphical representation. Also, keep in mind that flowcharts are an old way to represent an algorithm used very often in the imperative programming style in the past. A good thing about flowcharts is that they clearly show how control structures operate.

I understand the the shape of the boxes represent diffrent things i.e the top box, is speciffically for 'start/end' and the dimond box is for 'decisions' ect but I more wondering about how you draw them and what type of information you put in each box.
Additionally to what has already been said and the resources given, if you want to take a look at a very simple example of a problem solved using a flowchart you can take a look at my tutorial.

Klystron and sysprog
Mark44
Mentor
Doesn't python do for loops by iterating over a list?
Yes, or a range, such as in this example:
Python:
for i in range(11):
print("i = ", i)
The output for this for loop looks like this (some output lines omitted):
i = 0
i = 1
i = 2
...
i = 10

PeterDonis
Mentor
2019 Award
Doesn't python do for loops by iterating over a list?
A Python for loop can iterate over lots of different kinds of things. The general term is "iterable"; basically it's anything that has an __iter__ method, or anything you can call the iter() builtin function on and not get an error. Examples other than lists are dicts, sets, ranges, and generators, plus the return values of lots of the functions in the itertools module. (Many of these iterables are infinite, which means if you iterate over them with a for loop and don't have a condition somewhere inside the loop that leads to a break statement, your program will never stop.)

Klystron
.Scott
Homework Helper
I am currently learning python and to understand the my code fully and to make notes I am trying to draw flow charts to compliment my code. ...

...

What I would like to know is there a general way to do flow charts for or is it up to the person who designs the program? I understand the the shape of the boxes represent different things i.e the top box, is specifically for 'start/end' and the diamond box is for 'decisions' etc but I more wondering about how you draw them and what type of information you put in each box.
As @anolunda said, it's like writing a sentence. Both the rules and the narrative adapt to the purpose.

Flow charts predate computer programming by a lot. The "diamond" was originally for a decision made by a person - not a computer. And, of course, flow charts are still used to describe procedures for people.
In general, flow charts are intended to be agnostic to the computer language - looking to describe only the approach to the problem. But if the intent it to describe a specific implementation, they can be very language specific.

The keys are purpose and communication.
Your stated purpose is: "I am currently learning python and to understand the my code fully and to make notes I am trying to draw flow charts to compliment my code.". Since you will be the reader, you need to determine what level of comprehension you want to presume for yourself. If you have mastered Python loops, then you may only need these three components:
- 1) a box that says "Loop on each element of the 'whatever' array", followed by
- 2) a diamond that checks for the end of loop condition;
- 3) followed by the body of the loop; then
- 4) a path leading back up to the diamond.

On the other hand, if you are looking to document the details of the loop, you may have several boxes describing how the loop is set up, a box and diamond for the "next" and "exit" functions, and then the body of the loop.

I have also found the diamond to be a little inefficient when it comes to space. It doesn't provide a lot of room for text inside, and putting the text outside the diamond is sloppy. So creating a single "next, check for exit" rectangle which drops into the body or exits to the side is also fair play.

Flow charts have always been popular for describing how the human operators should operate the program - or for high level descriptions of what a computer system is doing.
But there was a time when flow charts were often required programming documentation. That fad didn't last very long. But in the 1980's I remember preparing detailed flowcharts to RADC (then part of the US DoD) and asking them if they really wanted the specified 20 copies - being that each copy would be a 16-inch stack of 8.5x11-inch paper.
They opted for only 6 copies and I am certain that those flowcharts were never used again.

Klystron, anorlunda and sysprog
anorlunda
Staff Emeritus
But in the 1980's I remember preparing detailed flowcharts to RADC (then part of the US DoD) and asking them if they really wanted the specified 20 copies - being that each copy would be a 16-inch stack of 8.5x11-inch paper.
I think there were numerous examples of forced excessive documentation. Not only was it not read, but in the maintenance phase, the burden of maintaining the documentation proved to be overwhelming.

But coming back to this thread, how much flowchart detail is enough? I say one page. A one page flow chart will be read and understood. More than that and the probability of reading/understanding drops rapidly. You can make a one page flowchart for a simple for loop or for putting a man on the moon. Simply adjust the level of detail.

Mark44
Mentor
From post #1:
I am currently learning python and to understand the my code fully and to make notes I am trying to draw flow charts to compliment my code.
A question you should ask yourself is"why draw a flow chart?"
As @.Scott pointed out, flow charts were used before programming languages came along. Many textbooks for programming languages don't even bother with flow charts, but use pseudocode instead. Here's a wikipedia article on pseudocode, with examples in several styles: https://en.wikipedia.org/wiki/Pseudocode

anorlunda
symbolipoint
Homework Helper
Gold Member

Diamond - Begins a decision structure

Lengthened hexagon - Begins a loop, but some other shape might be chosen

Lengthened vertical line ended with a small circle - shows dropping out from the loop when the loop session is finished, but other structural pieces might be chosen

Rectangle with upper left corner "cut off" - user input for variables' values, but other shape might be chosen

Taylor_1989
symbolipoint
Homework Helper