Programming Constructs (Sequence, Selection, Iteration) Revision Notes
Subject: Computer Science | Level: GCSE | Exam Board: OCR
Master the three fundamental building blocks of all programs: Sequence, Selection, and Iteration. This guide will equip you with the core knowledge to excel in OCR GCSE Computer Science Paper 2, turning abstract concepts into concrete marks."
Revision Notes & Key Concepts
Worked Examples
Worked Example
Question: Write an algorithm that asks the user to enter a password. The algorithm should keep asking for the password until the user enters "secret". Once the correct password is entered, it should display the message "Access granted".
Solution: Step 1: Initialise a variable to store the user's input. We can set it to an empty string to start. `password = ""` Step 2: Use a condition-controlled loop (`WHILE`) that continues as long as the entered password is not equal to "secret". `while password != "secret":` Step 3: Inside the loop, prompt the user to enter the password and store their input in the `password` variable. ` password = input("Enter password: ")` Step 4: After the loop finishes (which only happens when the password is correct), print the success message. `print("Access granted")` **Final Algorithm:** python password = "" while password != "secret": password = input("Enter password: ") print("Access granted")
Worked Example
Question: A teacher needs a program to calculate the average score for a class of 30 students. Write an algorithm that allows the teacher to enter 30 scores, and then displays the total score and the average score.
Solution: Step 1: Initialise a variable `totalScore` to 0. This will be used as a running total. `totalScore = 0` Step 2: Use a count-controlled loop (`FOR`) that runs exactly 30 times. `for i in range(30):` Step 3: Inside the loop, prompt the user to enter a score. The input must be converted to an integer. ` score = int(input("Enter score: "))` Step 4: Add the entered score to the running total. ` totalScore = totalScore + score` Step 5: After the loop, calculate the average by dividing the total by 30. `averageScore = totalScore / 30` Step 6: Display the total and average scores. `print("Total score is: ", totalScore)` `print("Average score is: ", averageScore)` **Final Algorithm:** python totalScore = 0 for i in range(30): score = int(input("Enter score: ")) totalScore = totalScore + score averageScore = totalScore / 30 print("Total score is: ", totalScore) print("Average score is: ", averageScore)
Worked Example
Question: Write an algorithm to check if a number entered by a user is positive, negative, or zero. The program should print a different message for each case.
Solution: Step 1: Prompt the user to enter a number and store it as an integer. `number = int(input("Enter a number: "))` Step 2: Use an `IF` statement to check if the number is greater than 0. `if number > 0:` ` print("The number is positive.")` Step 3: Use an `ELIF` statement to check if the number is less than 0. `elif number < 0:` ` print("The number is negative.")` Step 4: Use an `ELSE` statement to catch the only remaining possibility: the number is zero. `else:` ` print("The number is zero.")` **Final Algorithm:** python number = int(input("Enter a number: ")) if number > 0: print("The number is positive.") elif number < 0: print("The number is negative.") else: print("The number is zero.")
Practice Questions
Question: State the most appropriate programming construct (sequence, selection, or iteration) for each of the following scenarios.
Answer:
Question: A program needs to ask a user for their favourite colour 5 times. Which type of loop is most appropriate and why?
Answer:
Question: Identify the error in the following algorithm. Explain why it is an error.
Answer:
Question: Write an algorithm that asks the user to enter numbers. The user can enter as many numbers as they like. To stop, they must enter -1. The algorithm should then output the total of all the numbers entered (not including -1).
Answer:
Question: Compare the use of a FOR loop and a WHILE loop in programming. You should refer to the terms count-controlled and condition-controlled in your answer.
Answer:


