10.5. Flowcharts Describing Loops

Flowcharts show the flow of a program graphically. Flow charts were introduced in the previous chapter to describe how a programs that include if statements are illustrated graphically.

This chapter is about loops. Flowcharts can also be used to describe programs which contain for loops and while loops.

10.5.1. Basic Flow Chart Shapes

Let’s review the four basic flowchart shapes. Each shape represents a different type of operation.

  • oval: start and end

  • parallelogram: input and output

  • rectangle: calculations

  • diamond: selection structures

Four the four flowchart shapes: oval, parallelogram, rectangle, and diamond

The basic shapes in a flowchart are connected by arrows. The shapes and arrows in a flowchart represent the flow of a program from start to end.

10.5.2. Flowchart of a program that contains a for loop

Below is the description of a program that can be coded with a for loop:

The program starts. The program prints the word “looping” 10 times. Finally, the program ends.

A flowchart that describes this program is shown.

Flowchart of a program that contains a for loop

The Python code that corresponds to this flowchart is below:

# start
for i in range(10):
    print("looping")
# end

10.5.3. Flowchart of a program that contains a while loop

Below is the description of a program which can be coded with a while loop:

The program starts. The program asks the user for a positive number. If the number is negative, the program asks the user for a positive number again. If the number is positive, the program prints “positive”. Finally, the program ends.

A flowchart that describes this program is shown.

Flow chart of a program that contains a for loop

The Python code that corresponds to this flow chart is:

# start
num = -1
while num < 0:
    num = input("Enter a positive number: ")
    num = float(num)
print("positive")
# end