Loading...
Programs execute statements in an order and choose branches from logical conditions. Trace the values at the moment a condition is tested, rather than reading code as an informal description.
What you'll be able to do
In an imperative program, statements normally execute in sequence unless control flow changes it. Assignment evaluates an expression using the current state and updates the target. An expression such as total=total+price is an update, not a mathematical claim that price equals zero.
Reordering assignments can change results. Record the new value after each statement in a trace table and distinguish assignment from equality comparison. Python uses = for assignment and == for equality, while pseudocode may use a left arrow; explain the convention before tracing.
Relational operators compare values: equality, inequality and ordered comparisons. NOT reverses a Boolean result; AND requires both conditions; OR accepts either or both; XOR accepts exactly one of two Boolean conditions. Parentheses make the intended grouping visible.
A permitted age range from 12 to 18 inclusive needs age>=12 AND age<=18. Using OR accepts almost every number because a value outside one side still satisfies the other. Check both boundary values and values immediately outside the permitted range.
An if/elif/else chain selects the first satisfied branch and then leaves that chain. Two separate if statements may both execute. Nested selection makes a second decision within one branch; indentation in Python determines which branch contains each statement.
Overlapping conditions should be placed deliberately. Testing score>=50 before score>=80 in the same chain can make the higher-grade branch unreachable. Test the most restrictive relevant case first, or define non-overlapping intervals, then trace representative inputs.
score = 83
if score >= 80:
grade = "Distinction"
elif score >= 50:
grade = "Pass"
else:
grade = "Retry"
print(grade)Scroll horizontally to see the full code or table.Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.