Programming Constructs (OCR A-Level Computer Science): Revision Notes
Programming Constructs
Overview
In programming, sequence, iteration, and branching are the three fundamental constructs that form the basis of all algorithms. These constructs allow programmers to control the flow of a programme, enabling it to make decisions, repeat tasks, and execute instructions in a specific order.
Understanding and effectively using these constructs is essential for developing efficient and correct programmes.
Sequence
Definition:
-
Sequence is the simplest construct, where instructions are executed one after another in the order they are written. Purpose:
-
Ensures that tasks are completed in a specific order, which is critical for programmes where the output of one step depends on the completion of the previous step.
Example in Pseudocode:
INPUT name
PRINT "Hello, " + name
PRINT "Welcome to the programme!"
Example in Python:
name = input("Enter your name: ")
print("Hello, " + name)
print("Welcome to the programme!")
Iteration
Iteration is the process of repeating a set of instructions until a certain condition is met.
Types of Iteration:
Condition-Based Iteration (While Loop):
Repeats as long as a condition is true.
Example in Pseudocode:
WHILE count < 5
PRINT count
count = count + 1
ENDWHILE
Example in Python:
count = 0
while count < 5:
print(count)
count += 1
Count-Controlled Iteration (For Loop):
Repeats a specific number of times.
Example in Pseudocode:
FOR i = 1 TO 5
PRINT i
ENDFOR
Example in Python:
for i in range(1, 6):
print(i)
Repeat-Until Loop (Post-Test Loop):
Executes the loop body at least once and repeats until a condition is met.
Example in Pseudocode:
REPEAT
INPUT value
UNTIL value > 0
Branching
Branching allows a programme to make decisions and choose between different paths based on conditions.
Types of Branching:
IF Statement:
Executes a block of code if a condition is true.
Example in Pseudocode:
IF score >= 50 THEN
PRINT "You passed!"
ENDIF
Example in Python:
if score >= 50:
print("You passed!")
IF-ELSE Statement:
Provides an alternative action if the condition is false.
Example in Pseudocode:
IF score >= 50 THEN
PRINT "You passed!"
ELSE
PRINT "You failed."
ENDIF
Example in Python:
if score >= 50:
print("You passed!")
else:
print("You failed.")
IF-ELSEIF-ELSE Ladder:
Allows checking multiple conditions.
Example in Pseudocode:
IF score >= 80 THEN
PRINT "Grade A"
ELSEIF score >= 60 THEN
PRINT "Grade B"
ELSE
PRINT "Grade C"
ENDIF
Example in Python:
if score >= 80:
print("Grade A")
elif score >= 60:
print("Grade B")
else:
print("Grade C")
CASE/SWITCH Statement:
Simplifies multiple condition checks by matching a value to predefined cases.
Example in Pseudocode:
CASE day OF
"Monday": PRINT "Start of the workweek."
"Friday": PRINT "Almost the weekend!"
"Saturday", "Sunday": PRINT "It's the weekend!"
ENDCASE
Note: Python does not have a direct switch statement but can use if-elif or dictionaries for similar functionality.
Combining Constructs
In real-world programmes, sequence, iteration, and branching are often combined to solve complex problems.
Example: Simple ATM System
Pseudocode:
START
BALANCE = 1000
WHILE TRUE
PRINT "1. Check Balance"
PRINT "2. Withdraw Money"
PRINT "3. Exit"
INPUT choice
IF choice == 1 THEN
PRINT "Balance: ", BALANCE
ELSEIF choice == 2 THEN
PRINT "Enter amount to withdraw:"
INPUT amount
IF amount <= BALANCE THEN
BALANCE = BALANCE - amount
PRINT "Withdrawal successful."
ELSE
PRINT "Insufficient funds."
ENDIF
ELSEIF choice == 3 THEN
PRINT "Goodbye!"
BREAK
ELSE
PRINT "Invalid choice."
ENDIF
ENDWHILE
Benefits of Using Constructs
- Sequence: Ensures tasks are completed in the correct order.
- Iteration: Automates repetitive tasks, reducing redundancy.
- Branching: Enables dynamic decision-making, allowing the programme to handle different inputs and scenarios.
Note Summary
Common Mistakes
- Sequence Errors: Skipping or misordering steps can cause incorrect results.
- Infinite Loops in Iteration: Failing to update conditions in loops can cause programmes to run indefinitely.
- Incorrect Conditions in Branching: Miswriting conditions can lead to incorrect or unintended outcomes.
Key Takeaways
- Sequence: Executes instructions in order.
- Iteration: Repeats tasks using loops (
WHILE,FOR,REPEAT UNTIL). - Branching: Makes decisions using
IF,IF-ELSE,CASE, orSWITCH. - Effective programming requires understanding and combining these constructs to solve problems efficiently.