6.2.1 Program structural components
Building Blocks of Every Program
All programs — regardless of language — are built from a fixed set of structural components. Understanding these components in code allows you to read any program and identify what each part does, and to write programs by combining them deliberately.
Components Reference
A variable is a named memory location whose value can change during execution. A constant is a named value that does not change.
# Python
score = 0 # variable: value will change
MAX_SCORE = 100 # constant: value should not change
name = "Alice" # variable: stores a string
// C#
int score = 0;
const int MAX_SCORE = 100;
string name = "Alice";
An initialisation statement gives a variable its first value when it is created. An assignment statement updates a variable's value at any point.
# Python
total = 0 # initialisation
total = total + 10 # assignment (update)
total = total * 2 # assignment (update again)
In Python, the same syntax (=) is used for both. In C# and VB.NET, type declarations distinguish initialisation from later reassignment.
A command sequence is a list of statements executed in order, one after another, from top to bottom. Sequence is the default flow in a program — every program begins as a sequence, with selection and repetition layered on top.
# Python — a sequence of three assignments and a print
width = 8
height = 5
area = width * height
print("Area:", area) # statements run in this exact order
Selection executes different code depending on a condition. Implemented with if / elif / else in Python.
# Python
grade = int(input("Enter score: "))
if grade >= 80:
print("A")
elif grade >= 60:
print("B")
elif grade >= 40:
print("C")
else:
print("Fail")
// C#
if (grade >= 80) Console.WriteLine("A");
else if (grade >= 60) Console.WriteLine("B");
else if (grade >= 40) Console.WriteLine("C");
else Console.WriteLine("Fail");
Repetition executes a block of code multiple times. Two types:
- Count-controlled (for loop): repeats a fixed number of times
- Condition-controlled (while loop): repeats while a condition is true
Iteration is repeating over every item in a data structure:
# Python
# Count-controlled
for i in range(5):
print(i)
# Condition-controlled
count = 0
while count < 5:
print(count)
count = count + 1
# Iteration over every item in a list
scores = [70, 85, 60, 90]
for score in scores:
print(score)
Subprograms are named blocks of reusable code — either procedures (no return value) or functions (return a value). Parameters are the inputs a subprogram accepts.
# Python
def greet(name): # name is a parameter
"""Procedure: prints a greeting — no return value."""
print("Hello,", name)
def square(n): # n is a parameter
"""Function: returns the square — has a return value."""
return n * n
greet("Alice") # calls procedure: prints Hello, Alice
result = square(7) # calls function: result = 49
// C#
void Greet(string name) { Console.WriteLine("Hello, " + name); }
int Square(int n) { return n * n; }
Input reads data from the user; output displays results. Data structures store multiple related values.
# Python — input/output
name = input("Enter your name: ") # input
print("Hello,", name) # output
# 1D list (array)
scores = [70, 85, 60, 90]
print(scores[0]) # 70
# 2D list
grid = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
print(grid[1][2]) # 6 (row 1, column 2)
# Record (list used as a named structure)
student = ["Alice", 17, 85] # [name, age, score]
print(student[0], "scored", student[2])
Complete Annotated Example
MAX_SCORE = 100 # constant
def get_grade(score): # subprogram (function); score = parameter
"""Returns a letter grade."""
if score >= 80: # selection
return "A"
elif score >= 60:
return "B"
else:
return "C"
scores = [] # data structure (list); initialisation
for i in range(3): # repetition (count-controlled)
s = int(input("Score: ")) # input; s is a variable
scores.append(s) # assignment (updating the list)
for s in scores: # iteration over data structure
print(s, "->", get_grade(s)) # output; subprogram call
Key Takeaways
- Variable: changes; constant: fixed; both are named identifiers.
- Initialisation: first value; assignment: any update.
- Sequence: top-to-bottom order. Selection: if/elif/else.
- Repetition: count-controlled (for) or condition-controlled (while). Iteration: over every item in a structure.
- Subprograms: function (returns value) or procedure (no return). Parameters: inputs to a subprogram.
- Data structures: 1D list, 2D list (grid), record (list with named roles).