Programming Constructs (OCR GCSE Computer Science): Revision Notes
Programming Constructs
In programming, constructs are the fundamental building blocks that control the flow of a programme. The three basic programming constructs are sequence, selection, and iteration. Each of these constructs helps dictate the order in which instructions are executed in a programme. Understanding these concepts is essential for writing programmes that make decisions, repeat tasks, and follow logical steps. Let's break down each construct and look at examples to help you understand how they work in real-world programming.
Sequence
- Sequence refers to the execution of instructions in the exact order they are written, one after the other.
- A sequence is the simplest way a programme runs, where each line of code is executed in a straight line, top to bottom, like following a list of instructions. Example:
print("Enter your details")
name = input("Enter your name: ")
print("Hello, " + name)
Explanation:
- The programme first prints "Enter your details" to the screen.
- Then, it waits for the user to type their name and stores it in the variable
name. - Finally, it prints a greeting message using the name the user entered.
- Each line is executed one after the other in the order they appear.
Key Points
- Sequence ensures that each instruction is executed exactly once and in the right order.
- It is the foundation for all other constructs and is used in every programme.
Selection
- Definition: Selection allows the programme to choose between different paths of execution based on a condition, often using if, elif, and else statements.
- Understanding: Selection allows your programme to make decisions based on conditions. It checks if something is true or false, and depending on that, it takes different actions. Example:
age = int(input("Enter your age: "))
if age >= 18:
print("You are an adult")
else:
print("You are a minor")
Explanation:
- First, the programme asks the user to enter their age and converts it to an integer.
- Then, it checks if the age is greater than or equal to 18.
- If it is, the programme prints "You are an adult".
- Otherwise, it prints "You are a minor".
- This way, the programme chooses what to do based on the user's input.
Key Points
- The selection allows programmes to make decisions based on conditions, changing the flow of execution.
- It's crucial to create programmes that respond differently to different inputs or situations.
Iteration
Iteration involves repeating a block of code multiple times. There are two main types of loops used in programming: count-controlled loops (using for loops) and condition-controlled loops (using while loops).
Count-controlled Loop (For Loop)
- A loop that repeats a specific number of times, often using a for loop.
- Use this when you know in advance how many times you need to repeat something. It loops a certain number of times, counting each time it runs. Example:
for i in range(5):
print("This is loop iteration:", i)
Explanation:
- The for loop runs 5 times, starting with i set to 0 and increasing by 1 each time (up to 4).
- Each time the loop runs, it prints the message "This is loop iteration:" followed by the current value of i.
- So it prints the message 5 times in total, with i showing 0, 1, 2, 3, and 4.
Key Points
- The loop runs a fixed number of times, defined by the range or count provided.
- Count-controlled loops are great for repeating tasks a set number of times, like printing values or processing lists.
Condition-controlled Loop (While Loop)
- A loop that repeats as long as a specific condition remains true, often using a while loop.
- Use this loop when you don't know in advance how many times it will run. It keeps repeating until a certain condition becomes false. Example:
number = 0
while number < 3:
print("The number is", number)
number += 1
Explanation:
- The loop starts with number set to 0.
- It will keep running as long as number is less than 3.
- Each time the loop runs, it prints the current value of number and then adds 1 to number.
- Once number reaches 3, the condition becomes false, and the loop stops.
Key Points
- Condition-controlled loops are useful when you don't know exactly how many times a loop will run and want it to continue until a condition is no longer true.
- Always make sure the condition will eventually become false, or you'll create an infinite loop, which never stops!
Practical Use
- Sequence: Students use sequence in simple programmes, like printing messages in a specific order or creating a programme that asks for user details step-by-step.
- Selection: Students use if statements in programmes that make decisions, like checking if a user is old enough to access certain content or giving different messages depending on test scores.
- Count-controlled Loops: Students use for loops to repeat actions a set number of times, like going through a list of items, or creating countdowns or times tables.
- Condition-controlled Loops: Students use while loops in programmes that need to repeat until a specific condition is met, like guessing games that keep asking until the correct answer is given or a quiz that runs until all questions are answered.
Key Points to Remember
- Sequence ensures that instructions are executed in a specific, logical order.
- Selection allows decision-making in a programme using if statements to control different outcomes.
- Iteration repeats sections of code either a specific number of times (count-controlled) or until a condition is met (condition-controlled).