Skip to content
IGCSE·Tuition
Computer Science · Lessons

Explain an expected result before running code

You run the program, read the output and decide it looks right, which is the quickest way to miss a fault.

On this page
  1. How do I work out an expected result?
  2. Worked example
  3. The mistake to watch for
  4. Check yourself
  5. Where does this lead next?

An expected result is what a program should output for a given input, worked out from the requirement or by tracing the algorithm before it runs. It is the thing a test is compared against. If you skip this step, a wrong output can look reasonable.

This lesson follows designing test data and belongs to the validation, verification and testing module.

How do I work out an expected result?

Use a trace table. Write the variables as columns. Start with their initial values, then follow each line in order and record every change.

If a requirement is given in words, you can also reason directly from it. Either way, write the expected value down first.

Worked example

A fictional teacher counts how many of five test marks are a pass. The pass mark is 50 or more.

Total ← 0
FOR Count ← 1 TO 5
  INPUT Mark
  IF Mark >= 50 THEN
    Total ← Total + 1
  ENDIF
NEXT Count
OUTPUT Total

The five marks entered are 62, 48, 50, 91, 49.

Step 1, set up the table with columns Count, Mark, Mark >= 50, Total. Start with Total = 0.

Step 2, trace each iteration.

CountMarkMark >= 50Total
(start)0
162TRUE1
248FALSE1
350TRUE2
491TRUE3
549FALSE3

Step 3, read the output. After the loop, OUTPUT Total gives 3.

Step 4, check against the requirement. Passing marks are 62, 50 and 91. That is three marks, so the code and the requirement agree.

The note in the table matters: row 3 has Mark 50, and >= makes it a pass. That boundary value is where predictions most often slip.

The mistake to watch for

Mistaken prediction: “The output is 2, because 50 is only a pass if the mark is above 50.”

The student read >= as >.

The symbol >= means greater than or equal to, so 50 passes. The student’s table would show FALSE in row 3 and end at 2, which gives the wrong expected result. Slow down on comparison symbols and say them aloud: “greater than or equal to”.

After you write the prediction, you run the program. If it prints 3, the test passes. If it prints 2, you have found a fault in the code, because your prediction came from the requirement.

Check yourself

1. Trace this and state the output.

Sum ← 0
FOR N ← 1 TO 6
  IF N MOD 2 = 0 THEN
    Sum ← Sum + N
  ENDIF
NEXT N
OUTPUT Sum
Show answer

N MOD 2 = 0 is TRUE for 2, 4 and 6. Sum goes 0, 2, 6, 12. The output is 12.

2. The marks 70, 30, 50 are entered into the pass-counting algorithm above with the loop changed to run three times. What is the expected output?

Show answer

70 passes, 30 does not, 50 passes. The output is 2.

3. A student predicts 5 for question 2 because “there are five marks”. What went wrong?

Show answer

They used the loop count from the original program and counted every mark, not only the passes. The question has three marks, and only two meet the condition Mark >= 50.

Where does this lead next?

Next, learn to spot a branch of the code that no test has run. You can practise tracing in the restricted pseudocode trace trainer, and the mixed practice set mixes tracing with test design.

If your traces break down in loops, that is something we work on in online one-to-one Computer Science tuition.

Questions people ask

What is a dry run?

A dry run means following an algorithm by hand, line by line, recording the value of each variable in a trace table. You do it without a computer. It lets you predict the output and find logic errors before the program is ever run.

Why predict the result before running code?

If you only look at the output afterwards, it is easy to accept whatever appears. A prediction made from the requirement gives you something independent to compare with. A mismatch then tells you either the code or your understanding has a fault.

What goes in a trace table?

One column for each variable and for the output, and one row for each time a value changes or a line of interest runs. Write the starting values first, then update row by row. Include the condition result when the algorithm has an IF.

What if my predicted result and the actual result differ?

Do not just change the prediction. Trace the code again slowly, and check the requirement. Either you mis-traced a step, the code has a logic error, or your expected value was wrong. Finding which one is the useful part.

Updated:

Your next step

If your dry runs go wrong at one step you cannot find, a one-to-one teacher can watch you trace a fresh algorithm and point to the exact line where your picture of the code diverges.

Paid one-hour trial at your assigned teacher’s confirmed rate, starting from RM80. Other fees, schedules and ongoing arrangements are confirmed directly with your teacher after the trial class.

Tuition is arranged with a parent or guardian. Send them this page on WhatsApp and they can enquire for you.

Parent or guardian? Enquire here

9,000+ students helped through our service