Read a program by tracking values and control flow
Edited by: Hitoiki Arcade / Hitoiki Learning team · Published: · References and presentation updated: · Editorial policy
Following code with your eyes can leave you unsure of the values halfway through. Write down the inputs and current state, then update them after each executed instruction. Being able to explain which lines ran is more useful than guessing the final number.
This guide uses Python 3's specific rules. Other languages may use similar symbols with different meanings. Separating assignment, branch selection and loop termination makes the structure of a small program easier to understand.
Assignment evaluates before it updates
After x=4, executing x=x+3 evaluates the right-hand side using the current value of x and assigns the result back to x. It is not a mathematical assertion that both sides remain equal forever. Record the before and after values separately to see the operation clearly.
In Python, = assigns a value while == compares equality. One extra character changes the task. Describe each line as either updating a value or testing a condition so that comparison does not silently become assignment in your reasoning.
Each section link opens a quiz on related topics. Questions may cover more than that section, so check the range and difficulty before starting.
A selected branch changes what is considered next
In an if–elif chain, a true earlier condition selects its branch; later elif branches are not selected as well. If x is 8 and x>0 comes first, that branch wins. The fact that x>5 is also mathematically true does not mean both bodies run within this chain.
Try inputs that satisfy several conditions when examining a classification rule. Two independent if statements have a different structure from one if–elif chain. Even code containing the same tests can produce a different result when those tests are organized differently.
A Python for loop visits items in a sequence. Break exits the loop, whereas continue skips the remainder of the current iteration and proceeds to the next. Calling both a way to skip ahead misses the important difference in the scope of the action.
An else attached to a loop runs after normal completion without a break, assuming no exceptional exit. In a search that breaks on a match, the else can handle the no-match case. Track how execution ended rather than treating the visually last block as something that always runs.
Trace name assignment separately from object mutation
After a=[1]; b=a, both names refer to one list. b.append(2) changes that object, so a also reads [1,2]. Adding a name does not copy the contents. A useful sketch draws two arrows from the names toward the same list, rather than drawing two independent lists.
A shallow copy can create a new outer container while sharing objects inside it. A default-argument list is also created once at function definition, so later calls can see earlier additions. When independent state is needed, decide where a new object should be created. Record displayed output from print separately from a value returned to the caller; these follow different paths.
Two names, one object
Adding a name does not add a content copy. Check whether both references lead to the same object.
and evaluates left to right and skips its right operand when the left is false. With n!=0 and 10/n>2, zero stops the evaluation before division; reversing the order does not provide the same guard. while also checks before its body, so an initially false condition runs no iteration. Indentation identifies which statements belong within a conditional body.
A break inside nested loops ends the innermost loop rather than every enclosing traversal. Search exclusions also depend on premises: in ascending data, a middle value below the target proves that earlier values cannot be the target. Without sorting, that proof fails. Explain both the work skipped and the condition that makes skipping it justified.
Compare the choices before opening the answer and explanation. Reading an example does not save a test answer or score.
Example 1 · Programming and algorithms
After executing x=4; x=x+3 in order in Python 3, what is x?
4
3
An error due to contradictory equations
7
Read the answer and explanation
Answer: 7
The second line evaluates its right side using the previous value of x, which is 4. It calculates 4+3 and then assigns the result 7 back to x. This is an update, not a mathematical equation required to remain true forever.
Read reasoning and more detail
Trace the previous value, evaluated result and new value in separate steps to understand how the assignment changes state.
In Python 3, x=8. With if x>0: r='P', elif x>5: r='Q', else: r='R', what is r?
Input x=8 and the given if, elif and else code in top-to-bottom order. Condition truth and the selected branch are not shown. Comparisons are written in words to meet diagram text restrictions.
'P'
'Q'
'R'
'PQ'
Read the answer and explanation
Answer: 'P'
With x=8, x>0 is true, so the first body assigns 'P' and the later elif is not selected. An if–elif–else chain chooses the first qualifying branch, not every mathematically true condition. x>5 is also true as a separate comparison, but that does not make its branch run here. Two independent if statements would have a different control structure.
When does a Python 3 for-loop else clause run, assuming no exception, return or similar non-loop exit?
After a break exit (not ordinary exhaustion)
At each iteration start (not for empty input)
After iteration ends (including a break exit)
After completion without break (including empty input)
Read the answer and explanation
Answer: After completion without break (including empty input)
For normal completion, a for-loop else runs when the loop has not exited through break. In a search that breaks on a match, the match path skips the else; reaching the end without a match runs it. Its visual position after the loop does not make it unconditional.
Read reasoning and more detail
Trace both the item processing and the route by which the loop ends.
Shallow copies and left-to-right short-circuit evaluation.
Source checked: 2026-10-05
Continue with a quiz
Topics: Programming and algorithms. Range: Everyday knowledge, Broader knowledge, General knowledge. Difficulty: Basic, Standard. These are selected initially. You can change these on the setup screen.
This opens the setup screen with this subject and topic selected. Check the mode and question count before you start.
A test shows explanations after submission. Continuous challenge explains each answer. Review uses unresolved mistakes recorded on this device. Casual mode does not update learning records.