4.4. Flow of Execution of the for Loop

As a program executes, the interpreter always keeps track of which statement is about to be executed. We call this the control flow, or the flow of execution of the program. When humans execute programs, they often use their finger to point to each statement in turn. So you could think of control flow as “Python’s moving finger”.

Control flow until now has been strictly top to bottom, one statement at a time. We call this type of control sequential. In Python flow is sequential as long as successive statements are indented the same amount. The for statement introduces indented sub-statements after the for-loop heading.

Flow of control is often easy to visualize and understand if we draw a flowchart. This flowchart shows the exact steps and logic of how the for statement executes.

../_images/new_flowchart_for.png

A codelens demonstration is a good way to help you visualize exactly how the flow of control works with the for loop. Try stepping forward and backward through the program by pressing the buttons.

Python 3.3
1for i in range(0, 5, 1):
2    print("Hi")
Step 1 of 11
line that has just executed

next line to execute

Visualized using Online Python Tutor by Philip Guo
Frames
Objects

(vtest)

You have attempted 1 of 2 activities on this page
Next Section - 4.5. Iteration Simplifies our Turtle Program