CIE IGCSE | 7.7.1 Dry Runs, Variables, Outputs and User Prompts

Lesson objective

Dry-run an algorithm and record variable changes, supplied inputs, outputs and user prompts in an accurate trace table.

Learn

7.7.1 | DRY RUNS, VARIABLES, OUTPUTS AND USER PROMPTS

01 | WHAT IS A DRY RUN?

You do not always need to run a program on a computer to investigate its behaviour. You can follow an algorithm by hand, keeping track of its values as if you were executing each instruction yourself.

This is a dry run. A trace table records the changing values and important events during that execution.

Imagine reading a recipe and writing down what is in the bowl after every step. You do not jump straight to the finished cake; you follow the actual order. An algorithm trace works in the same way.

The Cambridge syllabus includes variables, outputs and user prompts when tracing an algorithm. This lesson introduces each kind of entry before combining them in a complete example.

Your task is to follow the algorithm given, even if it contains a mistake. A trace should reveal its actual behaviour, not silently repair it.

02 | PREPARE THE ALGORITHM AND INPUTS

Before tracing, identify the variable names and any starting assignments. Also identify the input values you are supposed to supply, in order.

Our first example receives two whole numbers and displays their sum. The input sequence will be 4, then 7.

1  DECLARE FirstNumber : INTEGER
2  DECLARE SecondNumber : INTEGER
3  DECLARE Total : INTEGER
4  OUTPUT "Enter the first number"
5  INPUT FirstNumber
6  OUTPUT "Enter the second number"
7  INPUT SecondNumber
8  Total ← FirstNumber + SecondNumber
9  OUTPUT "Total: ", Total

The line numbers are reference labels for this worked trace, not pseudocode instructions. They help us identify where each change occurs.

A declaration introduces a variable and its type. It does not assign zero. FirstNumber, SecondNumber and Total have no assigned numeric values yet at the start of this example.

Use a dash or a clear “not assigned” label where helpful; explain the notation rather than letting it look like a negative value.

03 | SET UP THE COLUMNS

Use a column for each relevant variable. An Event or Line column helps connect a row to the instruction, while Input and Output columns keep received and displayed information separate.

Some exam tables provide their own columns and conventions. Follow those instructions. Here we use a full-state style: each row shows the current values after that event, including unchanged values.

Dash means not assigned; no numeric starting value is assumed
EventFirstNumberSecondNumberTotalInputOutput
After declarationsNot assignedNot assignedNot assigned

The dash means no value has been assigned to that variable by this algorithm yet. Blank input or output cells mean no input or output occurred in that row.

A more compact trace can record only changed values. In that style, an empty variable cell means “no new assignment recorded”, not that its previous value disappeared. The meaning of the table convention must remain clear.

04 | RECORD A PROMPT AS OUTPUT

Line 4 displays the literal text “Enter the first number”. That is a user prompt: a message telling the user what to supply.

It belongs in the output record because the algorithm communicates it. It does not change FirstNumber, and it does not itself receive the value 4.

Line 5 is INPUT FirstNumber. Only now does the first supplied input enter the algorithm, giving FirstNumber the value 4.

Keep these two events separate. OUTPUT asks or tells; INPUT receives. A prompt can appear immediately before an input, but they are not the same instruction.

A common mistake is to omit prompts because they are not final answers. If asked for all output, include the prompts in their actual order.

05 | FOLLOW THE COMPLETE ADDITION TRACE

After the first input, line 6 displays the second prompt. Line 7 receives the next supplied value, 7, into SecondNumber. Line 8 calculates 4 + 7 and stores 11 in Total.

Full-state trace after each event; declarations precede these rows
Line / eventFirstNumberSecondNumberTotalInputOutput
4: promptNot assignedNot assignedNot assignedEnter the first number
5: input4Not assignedNot assigned4
6: prompt4Not assignedNot assignedEnter the second number
7: input47Not assigned7
8: addition4711
9: result4711Total: 11

Line 9 outputs the message and current total. It does not assign a new value to Total or change the inputs.

The full output sequence is: “Enter the first number”, then “Enter the second number”, then “Total: 11”. The received values 4 and 7 are inputs, not additional OUTPUT instructions.

The table follows execution, not just the order of variable names. It records the calculation only when that instruction is reached.

06 | ASSIGNMENTS USE CURRENT VALUES

Now trace three short assignments. Assume X and Y are declared INTEGER variables.

X ← 5
X ← X + 2
Y ← X * 3

X first becomes 5. The next instruction reads the current 5, adds 2 and stores 7 back in X. Y then becomes 7 multiplied by 3, which is 21.

Only the assigned variable changes at each step
InstructionX afterwardsY afterwards
X ← 55Not assigned
X ← X + 27Not assigned
Y ← X * 3721

Do not treat X ← X + 2 as an impossible algebraic equation. The arrow means update: evaluate the right-hand side, then store its result on the left.

When X changes, Y does not automatically recalculate. If a later line changes X to 9, the existing Y remains 21 until another instruction assigns Y a new value.

07 | LITERAL TEXT AND VARIABLE OUTPUT ARE DIFFERENT

If X holds 7, OUTPUT X displays 7. OUTPUT "X" displays the literal letter X.

OUTPUT "Value: ", X combines fixed text with the current value. Its output can be recorded as “Value: 7”. The comma separates items; it is not an extra comma that must appear in the message.

Read quotation marks carefully. A variable name inside quotes is just text; outside quotes it refers to the stored value.

Use the code’s message accurately, but do not invent additional output. Assigning seven to X does not display seven unless an OUTPUT instruction communicates it.

The system’s input interface may echo typed characters in real use. In a pseudocode trace, distinguish that from the explicit outputs the algorithm specifies.

08 | INPUTS ARE CONSUMED IN ORDER

A provided input sequence supplies values when INPUT instructions execute. The first INPUT consumes the first value, the next INPUT the second, and so on.

Suppose the algorithm performs INPUT A, INPUT B and INPUT A using inputs 4, 7 and 9. A becomes 4, B becomes 7, then A is replaced by 9.

The final state is A = 9 and B = 7. The third input does not create a new variable just because an earlier input used A already.

Do not consume an input value during an OUTPUT prompt, a comparison or an assignment. Only the executed input instruction receives the next supplied value.

If a trace reaches another INPUT but no value has been supplied for it, the run needs more input before it can continue. Do not invent a convenient value.

09 | FOLLOW THE CHOSEN PATH, NOT EVERY WRITTEN LINE

A decision can skip instructions. Trace this fragment with Mark equal to 4.

IF Mark >= 5
  THEN
    OUTPUT "Target reached"
  ELSE
    OUTPUT "Keep practising"
ENDIF

The condition Mark ≥ 5 is false, so only the ELSE output executes. Record “Keep practising”, not both messages.

With Mark equal to 5, the condition is true, so record “Target reached”. Equality is included by the comparison.

You can add a Condition result column when it helps explain a route. Some supplied tables do not require that column; you should still evaluate the condition to follow the correct path.

The next lesson develops selection and loops in depth. For now, remember that a trace follows the execution path determined by the current values.

10 | KEEP PROMPTS AND OUTPUTS IN ORDER DURING A RETRY

A short range-input loop asks for a whole-number mark until it lies from zero to fifty. Assume every supplied input is already a whole number.

DECLARE Mark : INTEGER
// Assume every supplied input is a whole number
REPEAT
    OUTPUT "Enter a mark from 0 to 50"
    INPUT Mark
    IF Mark < 0 OR Mark > 50
      THEN
        OUTPUT "Outside range"
    ENDIF
UNTIL Mark >= 0 AND Mark <= 50
OUTPUT "Accepted: ", Mark

Use input sequence 60, then 34. The first prompt appears, 60 is received and the check fails, producing “Outside range”. The loop asks again; 34 is received, passes and is output as accepted.

Failed then successful range check; each prompt is explicit output
EventMark afterwardsInputOutput
First promptNot assignedEnter a mark from 0 to 50
First input6060
Rejected-range message60Outside range
Second prompt60Enter a mark from 0 to 50
Second input3434
Accepted result34Accepted: 34

The same variable is reused: Mark changes from 60 to 34. The rejected value does not remain as the current Mark after the second input.

The final output is not the only output. Both prompts and the rejection message belong in the sequence. The last successful condition check exits the loop without displaying an error.

11 | TRACE THE ACTUAL ALGORITHM, INCLUDING FAULTS

Suppose the addition line is accidentally Total ← FirstNumber - SecondNumber. With inputs 4 and 7, the trace must show Total = −3, even though the apparent task was addition.

A dry run describes what the instructions do. You can separately explain that subtraction conflicts with an addition requirement, but do not put 11 into the trace for the faulty line.

Similarly, do not silently give an uninitialised total a starting zero. If the algorithm uses a value before assigning it, identify the missing initialisation or record that the value is not determined by the supplied instructions.

A trace can reveal logic errors and unclear assumptions. It is evidence for debugging, not a licence to change the algorithm while following it.

12 | DRY RUNS AND EXECUTED TESTS PROVIDE DIFFERENT EVIDENCE

A dry run predicts behaviour from the stated instructions and chosen inputs. An executed test observes a particular implementation in its environment.

The two can be compared with the requirements. If a dry run shows the wrong result, the algorithm itself needs attention. If a correct design produces a different result when implemented, investigate the implementation and setup.

Neither a single trace nor a single test covers every possible input. Choose contrasting data and important limits to exercise different behaviour.

Do not label hand-calculated predictions as actual observations from a running program. The distinction from the previous lesson still matters.

13 | A RELIABLE TRACING ROUTINE

Start with the supplied inputs and initial state. Read one executed instruction, perform exactly its operation, update the relevant value and record any input or output.

For assignments, use current values. For decisions, select the appropriate branch. For loops, return to the specified check and continue until its stopping rule is met.

Keep previous variable values unless an instruction changes them. Check quotation marks, arithmetic and output order. Do not skip the last condition check merely because the intended result already looks complete.

When asked to explain a trace entry, connect it to the instruction: “Total becomes eleven because line eight adds the current values four and seven and assigns the sum to Total.”

You should now be able to record variables, inputs, outputs and prompts without confusing their roles. Next you will practise longer paths involving selection and repeated loops.

MATCH THE TRACE TERM

Terminology

Terminology

Dry run

Following an algorithm by hand using supplied data.

Trace table

A record of variable values and relevant input/output events during execution.

Variable

A named place for a value used by the algorithm.

Declaration

Introducing a name and type; it does not assign a starting numeric value.

Assignment

Evaluating the right-hand expression and storing the result using the left-hand name.

Current value

The value presently associated with a variable.

Input sequence

The ordered values supplied as executed INPUT instructions request them.

User prompt

An output message telling a user what to enter.

Output

Information communicated by an executed OUTPUT instruction.

Literal text

Characters written directly in quotation marks.

Full-state trace

A table convention repeating current values after each recorded event.

Change-only trace

A compact convention recording new assignments rather than repeating unchanged values.

Condition result

TRUE or FALSE, determining which execution path is followed.

Initialisation

Assigning a starting value before using it.

Unassigned value

A variable with no value yet assigned by the given algorithm.

Execution path

The sequence of instructions actually followed for the chosen data.

Retry

Repeating input after rejection.

Logic error

Instructions that produce behaviour inconsistent with the intended task.

Questions

Questions

CHECK YOUR UNDERSTANDING

Select all correct choices. Each exact set earns one point.

1. What does a dry run do?
2. Which statements about declarations are correct?
3. X is five. After X ← X + 2, what is X?
4. Which belong in the output sequence?
5. X contains seven. Which are correct?
6. Inputs are four, seven and nine for INPUT A, INPUT B, INPUT A. Final values?
7. With Mark four in the threshold selection, which output occurs?
8. Which improve an accurate trace?

EXPLAIN AND COMPARE

Write your answers before revealing each sample.

1. Describe a dry run and a trace table.

2. Explain why a declaration should not be traced as assigning zero.

3. Explain X ← X + 2 when X is five.

4. Compare INPUT and an output prompt.

5. Describe the complete output for the addition example with four and seven.

6. Explain why Y remains twenty-one if X later changes from seven to nine.

7. Explain why a faulty subtraction line must produce minus three in the trace for four and seven.

8. Compare a dry run with an executed test observation.

TRACE IT | PRICE AND QUANTITY

Use inputs four and three. Construct a full-state trace with Price, Quantity, Cost, Input and Output. Record every prompt. Explain why Cost does not change after Quantity is increased.

DECLARE Price : INTEGER
DECLARE Quantity : INTEGER
DECLARE Cost : INTEGER
OUTPUT "Enter price"
INPUT Price
OUTPUT "Enter quantity"
INPUT Quantity
Cost ← Price * Quantity
OUTPUT "Cost: ", Cost
Quantity ← Quantity + 1
OUTPUT "Quantity now: ", Quantity
Reveal the final state and output sequence

Final Price is 4, Quantity is 4 and Cost is 12. Outputs, in order: Enter price; Enter quantity; Cost: 12; Quantity now: 4. Cost was calculated before the increment and is not assigned again, so it remains twelve.

Flashcards

Flashcards

Click to flip. Select the ideas you need to revisit.

0 cards selected for revision.

    Selections are kept while this page is open.

    Workbook

    Workbook

    COMING SOON

    The workbook for 7.7.1 Dry Runs, Variables, Outputs and User Prompts is coming soon. Complete the matching task and written questions in your exercise book.