This study notes covers Practical Examples (GCSE Pseudocode & Python) within Iteration (Loops) for GCSE Computer Science. Revise Iteration (Loops) in 3.2 Programming for GCSE Computer Science with 15 exam-style questions and 8 flashcards. This topic shows up very often in GCSE exams, so students should be able to explain it clearly, not just recognise the term. It is section 5 of 8 in this topic. Use this study notes to connect the idea to the wider topic before moving on to questions and flashcards.
Practical Examples (GCSE Pseudocode & Python)
Example 1: Sum Numbers 1 to 100
GCSE Pseudocode:
total = 0
for i = 1 to 100
total = total + i
next i
print("Sum: " + total)
// Output: Sum: 5050
Python Equivalent:
total = 0
for i in range(1, 101):
total = total + i
print("Sum:", total)
# Output: Sum: 5050
Example 2: Guessing Game
secretNumber = 42
guess = 0
attempts = 0
while guess != secretNumber
guess = input("Guess the number: ")
attempts = attempts + 1
if guess < secretNumber then
print("Too low!")
elseif guess > secretNumber then
print("Too high!")
endif
endwhile
print("Correct! You took " + attempts + " attempts")
Example 3: Input Validation with DO-WHILE
do
age = input("Enter your age (0-120): ")
until age >= 0 AND age <= 120
print("Valid age entered: " + age)Practice questions for Iteration (Loops)
Which type of loop is best used when you know exactly how many times the loop should repeat?
Explain what an infinite loop is, state one cause of an infinite loop, and describe one consequence of running an infinite loop in a program.