Information & AI guides →

Information & AI · Programming and algorithms

Read a program by tracking values and control flow

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.

Try this topic in a quiz

This opens the setup screen with this subject and topic selected. Check the mode and question count before you start.

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.

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.

Read the route by which a loop ends

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.

TRY & READ

Check your understanding with examples

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?

  1. 4
  2. 3
  3. An error due to contradictory equations
  4. 7
Read the answer and explanation

Answer: 7

The right side uses the old value 4; the result 4+3 is then assigned to x.

Sources for this example

  • 3. An Informal Introduction to Python — Python 3.14.8 documentation

    3.1.1 assignment; original trace; 3.1.2 immutable strings; 3.1.2 indexing; 3.1.2 slicing; original trace; 3.1.3 assignment aliasing; original trace; 3.1.3 len; 3.1.3 shallow slice copy; original trace; 3.2 comparisons; 3.2 indentation; 3.2 truth values of sequences; 3.2 while; 3.2 while; original trace; Introduction comments

    Source checked: 2026-10-05
  • Independent derivation of original examples

    Stepwise derivations indexed by ID; separate references support external facts.

    Source checked: 2026-10-05 · Original authoring and derivation record

Example 2 · Programming and algorithms

In Python 3, x=8. With if x>0: r='P', elif x>5: r='Q', else: r='R', what is r?

  1. 'P'
  2. 'Q'
  3. 'R'
  4. 'PQ'
Read the answer and explanation

Answer: 'P'

The first condition is true, so its branch is selected and the later elif is skipped; ordering matters.

Sources for this example

  • 4. More Control Flow Tools — Python 3.14.8 documentation

    4.1 if; 4.1 if elif; original trace; 4.2 for; 4.2 for; original reset-location trace; 4.2 modifying collections; original trace; 4.3 range; 4.4 break; 4.4 continue; 4.5 else clauses on loops; 4.6 pass; 4.8 defining functions; 4.8 implicit None; 4.8 local symbol table; original trace; 4.8 recursion; original termination; 4.8 return; 4.9.1 in membership; 4.9.1 mutable defaults; original trace; 4.9.2 keyword arguments; 4.9.7 documentation strings

    Source checked: 2026-10-05
  • Independent derivation of original examples

    Stepwise derivations indexed by ID; separate references support external facts.

    Source checked: 2026-10-05 · Original authoring and derivation record

Example 3 · Programming and algorithms

For a Python 3 for loop with an else clause and no exception or other abnormal exit, when does else run?

  1. Only when break exits the loop
  2. Before every iteration
  3. Only when the first element must be 0
  4. When iteration finishes without break, including an empty iterable
Read the answer and explanation

Answer: When iteration finishes without break, including an empty iterable

The loop else corresponds to completion without break; a search that breaks on a match can use it for the no-match case.

Sources for this example

  • 4. More Control Flow Tools — Python 3.14.8 documentation

    4.1 if; 4.1 if elif; original trace; 4.2 for; 4.2 for; original reset-location trace; 4.2 modifying collections; original trace; 4.3 range; 4.4 break; 4.4 continue; 4.5 else clauses on loops; 4.6 pass; 4.8 defining functions; 4.8 implicit None; 4.8 local symbol table; original trace; 4.8 recursion; original termination; 4.8 return; 4.9.1 in membership; 4.9.1 mutable defaults; original trace; 4.9.2 keyword arguments; 4.9.7 documentation strings

    Source checked: 2026-10-05

Sources for this guide

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.

Try this topic in a quiz

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.

Enjoyed it? Share the link

Only the page address is shared. Your saves and results are not included.

Link to this page