Back to the full dot-point answer
SingaporeComputer ScienceQuick questions
Programming in Python
Quick questions on Python loops and iteration explained: O-Level Computing
8short Q&A pairs drawn directly from our worked dot-point answer. For full context and worked exam questions, read the parent dot-point page.
What is the for loop with range?Show answer
A for loop repeats once for each value in a sequence. With range, it repeats a known number of times:
What is the while loop?Show answer
A while loop repeats while a condition is true. Use it when you do not know in advance how many repetitions are needed:
What are avoiding infinite loops?Show answer
A while loop runs forever if its condition never becomes false. Make sure the body changes a value used in the condition (read new input, increase a counter, or shrink a value) so the loop can end.
What is an infinite while loop?Show answer
If the body never changes the condition, the loop runs forever; always update the tested variable.
What is wrong indentation?Show answer
Statements inside a loop must be indented; an un-indented line runs after the loop, not in it.
What is q1?Show answer
State what range(0, 10) produces. [2 marks]
What is q2?Show answer
Write a for loop that prints the numbers to . [2 marks]
What is q3?Show answer
Explain one way to stop a while loop running forever. [2 marks]