What is iteration in programming?
Iteration is the repetition of a block of instructions, often using loops, to execute the same code multiple times.
Name the two main types of loops in KS3 Computing.
Count-controlled loops (for loops) and condition-controlled loops (while loops).
What is a count-controlled loop?
A loop that repeats a fixed number of times, typically using a counter variable. In Python, a for loop is often used.
What is a condition-controlled loop?
A loop that repeats as long as a condition is true. In Python, a while loop is used.
What is a loop variable?
A variable used to control the number of iterations in a loop, often incremented or decremented each time the loop runs.
Give an example of a for loop in Python that prints numbers 1 to 5.
for i in range(1, 6):
print(i)
Give an example of a while loop in Python that prints numbers 1 to 5.
i = 1
while i ≤ 5:
print(i)
i = i + 1
What is an infinite loop?
A loop that never ends because its condition is always true or it has no exit condition. It can cause a program to hang.
How can you avoid an infinite loop?
Ensure the loop's condition eventually becomes false, for example by updating the loop variable inside the loop.
When should you use a for loop instead of a while loop?
Use a for loop when you know the exact number of iterations in advance. Use a while loop when the number of iterations depends on a condition.
What is the purpose of the range() function in Python?
It generates a sequence of numbers, often used in for loops to specify the number of iterations.
What does range(5) produce?
It produces the numbers 0, 1, 2, 3, 4 (five numbers starting from 0).
What does range(1, 5) produce?
It produces the numbers 1, 2, 3, 4 (stops before 5).
What does range(1, 10, 2) produce?
It produces 1, 3, 5, 7, 9 (starts at 1, ends before 10, step of 2).
What is the difference between a for loop and a while loop?
A for loop iterates over a sequence or a fixed number of times; a while loop repeats as long as a condition is true.
How can you combine loops with selection (if statements)?
You can place an if statement inside a loop to make decisions for each iteration, e.g., printing only even numbers.
Write a Python loop that prints only even numbers from 1 to 10.
for i in range(1, 11):
if i % 2 == 0:
print(i)
What is a nested loop?
A loop inside another loop. The inner loop runs completely for each iteration of the outer loop.
Give an example of a nested loop that prints a 3x3 grid of asterisks.
for i in range(3):
for j in range(3):
print('×', end=' ')
print()
What is the role of the loop variable in a for loop?
It takes each value from the sequence (e.g., range) in turn, allowing you to use that value in the loop body.
What happens if you forget to update the loop variable in a while loop?
The loop may become infinite because the condition never becomes false.
What is the output of this code?
for i in range(3):
print(i)
0
1
2
What is the output of this code?
i = 0
while i < 3:
print(i)
i += 1
0
1
2
How do you break out of a loop early in Python?
Use the break statement. It immediately exits the loop and continues with the next statement after the loop.
What does the continue statement do in a loop?
It skips the rest of the current iteration and jumps to the next iteration of the loop.