Flow control is controlling the order in which statements or function calls of a program are executed.
There are three control structures: sequence, selection, and iteration.
Python has various control structures such as while loops, for loops, and if statements, which are used to determine which section of code is executed according to certain conditions.
Sequence
The interpreter will follow and execute each line of code in sequence until the end of the program.
Let’s have a look at a program. Open adder.py. This program has four statements.
Once you execute the program, the instructions are carried out in sequence .
Let’s try another example. Open inchestocm.py.
Selection
In most computer programs, there are certain points where a decision must be made. This decision is based on a condition, and that condition can be either true or false.
if... else
Let’s have a look at a program. Open selection.py. Here, we can see a very simple if statement to determine whether a test score is a pass or fail. The pass mark is 70, so we need an if statement to return a pass message if the value entered is greater than 70. Remember that we also need to cast the variable “mark” as an integer (int).
If you enter a value greater than 70, the Python interpreter will execute the first block of the “if statement .”
If you enter a value below 70, the Python interpreter will execute the “else block” of the “if statement .”
elif
Let’s have a look at a program. Open multiselection.py .
If we analyze the elif statement, we can see how it works. For the first condition, any number entered above 70 will execute the first block .
Any number between 60 and 69, the interpreter will execute the second block.
Similarly for the other conditions, 50–59 and 40–49.
If any condition is not met by the above “elif” statements, the interpreter will execute the “else” block at the end .
Iteration (Loops)
A loop is a set of statements that are repeated until a specific condition is met. We will look at two types of loops: the for loop and the while loop.
For Loop
Let’s have a look at a program. Open forloop.py. The for loop contains a loop condition. In this example, the loop will execute for each item in the fruitlist [sequence].
The “item” variable in the “for loop” statement is a pointer or counter to the current value or item in the sequence .
The interpreter will test the condition in the for loop again, and if it is true, the interpreter will execute everything inside the loop again. In each iteration of the loop, the counter moves to the next value or item.
At the end of the sequence, the loop condition becomes false, so the loop terminates .
Let’s look at another example. Open forloop2.py.
When you run through the program, you can see what it’s doing.
While Loop
Let’s have a look at a program. Open whileloop.py. The while loop contains a loop condition .
When you run through the program, you can see what it’s doing:
Break and Continue
Lab Exercises
- 1.
Write a program to print the numbers 1–10 to the screen.
- 2.
Write a program to print a list of names to the screen.
- 3.
Write a program to calculate and print the squares of the numbers from 1 to 10. Use tabs to display them in a table.
- 4.
Write a program that accepts a number from the user until a negative number is entered.
- 5.
Write a program that accepts an integer and prints the specified range it belongs to:
Range 1: 0 to 10
Range 2: 11 to 20
Range 3: 21 to 30
Range 4: 31 to 40
Summary
Flow control is controlling the order in which statements or function calls of a program are executed.
Sequence is the set of step-by-step instructions carried out in order to achieve a task or solve a problem.
Selection is the point at which a decision is to be made. For this, we use IF statements.
Iteration is where we need to repeat several lines of code multiple times. For this, we use WHILE and FOR loops.
Use FOR loop if you know how many times you’re going to execute the loop, such as processing a range or list.
If you need to repeat code until a condition is to be met, use a WHILE loop.
The break statement breaks out of a loop.