Practice Flashcards
Flip to reveal answersHow do you choose between a counted and a conditional loop?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All 5 Flashcards — Loops
Sign up free to track progress and get spaced-repetition review schedules.
Question
How do you choose between a counted and a conditional loop?
Answer
Ask whether you know how many times it must repeat before it starts. Yes means a counted loop (for); no means a conditional one (while), which repeats as long as a condition holds.
Question
How many times does a counted loop of 5 run, and what values does the counter take?
Answer
Five times, taking 0, 1, 2, 3 and 4. It starts at 0 and stops before 5 — the same rule as a string slice, and the same source of off-by-one errors.
Question
Can a while loop run zero times?
Answer
Yes. The condition is tested before the first pass, so if it is already false the body never runs at all. That is correct behaviour and often exactly what is wanted.
Question
Where should a counter or total be set up?
Answer
Before the loop. Setting it inside resets it on every pass, so the final answer would be just the last value rather than the accumulated one.
Question
Why do nested loops produce O(n²) work?
Answer
The inner loop runs completely for every single pass of the outer loop, so the total work is rows times columns. That is where quadratic complexity comes from.
Read the notes
Full study notes for Loops
Topic 6.3 hub
Programming constructs
More from Topic 6.3
All flashcards in this topic
Computer Science exam skills
Paper structures & tips
Track your progress with spaced repetition
Sign up free — Aimnova tells you exactly which cards to review and when, so you remember everything before your IB exam.
Start Free