Practice Flashcards
Why are sequence errors hard to spot?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All Flashcards in Topic 6.3
Below are all 20 flashcards for this topic. Sign up free to track your progress and get personalized review schedules.
6.3.15 cards
Why are sequence errors hard to spot?
Both lines are perfectly valid, so nothing is reported. The program runs to completion and simply produces the wrong answer.
What causes an infinite loop?
The thing the loop's test depends on never changes — most often because the counter is increased inside a branch that does not always run, so the test can never become false.
Where do setup, work and reporting belong relative to a loop?
Set up before the loop — totals at 0, counters at their start. Do the work for one item inside. Print or return the finished result after, since printing inside shows a running value rather than the final one.
What is deadlock and how is it prevented?
Two parts of a program each hold a resource the other is waiting for, so neither can move. It is prevented by having every part claim shared resources in the same fixed order.
What three questions check that a sequence is right?
Is every value set before it is used? Does the thing the loop tests always change? Is the result used after it is complete rather than during? Those three catch nearly every sequence error.
6.3.25 cards
What is the difference between an else-if chain and separate ifs?
A chain runs exactly one branch and skips the rest once a match is found. Separate ifs test every condition, so several may run or none. Use a chain for mutually exclusive cases and separate ifs for independent ones.
Why must a range chain be ordered narrowest first?
The chain stops at the first true test. If mark >= 50 is tested before mark >= 70, every mark of 70 or more matches the first test, so the later branch is unreachable.
What do and, or and not do?
and requires both sides to be true; or requires at least one; not reverses the result. They are the same logic as the AND, OR and NOT gates, with the same truth tables.
What is wrong with 'if day == "Sat" or "Sun"'?
The second half is a bare piece of text rather than a comparison, and non-empty text counts as true — so the whole condition is always true. Each side of an or must be a full comparison.
What is the difference between == and =?
== compares two values and produces a Boolean; = assigns a value to a variable. Writing = where == belongs is a classic error.
6.3.35 cards
How do you choose between a counted and a conditional loop?
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.
How many times does a counted loop of 5 run, and what values does the counter take?
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.
Can a while loop run zero times?
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.
Where should a counter or total be set up?
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.
Why do nested loops produce O(n²) work?
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.
6.3.45 cards
What is a function?
A named block of code that does one job, is given data through parameters, and hands back a result with return. Written once, it can be called anywhere and fixed in one place.
What is the difference between a parameter and an argument?
The parameter is the name in the function's definition — the placeholder. The argument is the actual value supplied when the function is called.
What are the benefits of modularisation?
Reusable: one definition serves every caller. Maintainable: a fix applies everywhere at once and faults trace to one function. Testable: each function can be tested on its own. And work can be split between people.
Why pass values in as parameters rather than using globals?
A function told what it needs works anywhere, can be tested on its own, and cannot be broken by something changing elsewhere in the program.
Why should a function return rather than print?
A returned value can be stored, compared or displayed by the caller, and the function can be tested. A function that only prints can do none of those.
Topic 6.3 study notes
Full notes & explanations for Programming constructs
Computer Science exam skills
Paper structures, command terms & tips
Want smart review reminders?
Sign up free to track your progress. Our spaced repetition algorithm will tell you exactly which cards to review and when.
Start Free