1.2.1 Sequence, selection, repetition, iteration
Programming Constructs Overview
Every algorithm — however complex — is built from just a few fundamental constructs: sequence, selection, repetition and iteration. These are combined with input, processing and output to solve problems. Understanding each construct and when to use it is the foundation of all programming.
Sequence
In a sequence, instructions are executed one after another, in the order they are written — from top to bottom. This is the default flow of any program. There is no branching or looping; every line runs exactly once.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print("Hello", name)
print("You are", age, "years old")
Each line runs in order. The print on line 3 cannot run before name is assigned on line 1.
Selection
Selection allows a program to make decisions — choosing which block of code to execute based on a condition. The condition evaluates to either True or False.
score = int(input("Enter score: "))
if score >= 50:
print("Pass")
The print only executes if score >= 50 is True. If the condition is false, nothing happens.
score = int(input("Enter score: "))
if score >= 50:
print("Pass")
else:
print("Fail")
Exactly one branch always runs — either "Pass" or "Fail", never both, never neither.
score = int(input("Enter score: "))
if score >= 70:
print("Distinction")
elif score >= 50:
print("Pass")
else:
print("Fail")
Multiple conditions are checked in order. The first True branch runs; the rest are skipped. elif can be used as many times as needed.
Repetition
Repetition (also called a loop) allows a block of code to run more than once. There are two types based on how the looping is controlled.
A while loop repeats as long as its condition remains True. The number of iterations is not known in advance — it depends on what happens during execution.
password = ""
while password != "secret":
password = input("Enter password: ")
print("Access granted")
The loop keeps asking until the correct password is entered. If the user types it correctly first time, the loop body runs once; it could run many times.
A for loop using range() repeats a fixed number of times. The number of iterations is determined before the loop starts.
for i in range(1, 6):
print(i)
This prints 1, 2, 3, 4, 5. Note: range(1, 6) generates 1 up to but not including 6.
total = 0
for i in range(1, 6):
total = total + i
print("Total:", total) # Output: Total: 15
Iteration (over a data structure)
Iteration in the PLS specifically refers to using a for loop to visit every element in a data structure (such as a list) one at a time. Unlike range(), this does not require knowing the index.
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
print(fruit)
Each element is assigned to the loop variable fruit in turn. The loop runs exactly once for every item in the list, regardless of how many items there are.
Input, Processing and Output
All algorithms combine these three stages:
| Stage | Description | PLS example |
|---|---|---|
| Input | Data entered by the user or read from a file | name = input("Name: ") |
| Processing | Calculations or decisions performed on the data | total = price * quantity |
| Output | Results displayed to the user or written to a file | print("Total:", total) |
Flowchart Symbols
Algorithms can be represented as flowcharts. The standard symbols are:
Key Takeaways
- Sequence: instructions run in order, top to bottom.
- Selection:
if/elif/else— one branch runs based on a condition. - Repetition:
while(condition-controlled) orfor range()(count-controlled). - Iteration:
for x in structure— visits every element of a data structure. - All algorithms are built from input → processing → output using these constructs.