Errors in algorithms
Testing a program means running it with inputs you choose in advance and comparing its actual output with the expected output you worked out by hand. You calculate what should come out in advance, then run the program and compare the two. A mismatch means there is a fault to find, and comparing actual with expected is the first stage of debugging.
The rule for this ride is that you must be at least 140 cm tall.
# checks whether someone is tall enough for the ride
height = int(input("Height in cm: "))
if height > 140:
print("Allowed on")
else:
print("Too short")Output when the user types 140:
Height in cm: 140
Too short
Entering 165 gives the answer you expected, so that test passes. Entering 140, the lowest allowed height, does not, so a fault is revealed: the condition should accept 140 itself, using >=. The passing test on its own told you nothing about it.
Vary the inputs on purpose rather than picking them at random. For a program that checks a username, which must be between 6 and 12 characters long and contain letters and digits only:
| Type of test data | Example input | Why it is used |
|---|---|---|
| Normal data | playerone (9 characters) | An ordinary length the program should accept. |
| Boundary data | abc123 (6 characters) and longusername (12 characters) | The shortest and longest allowed lengths, where length checks are most likely to be wrong. |
| Invalid data | abc12 (5 characters), thisiswaytoolong (16 characters) or bad!name | Values the program should reject: too short, too long, or a symbol that is not allowed. |
A runtime error often appears for just one input: the program works again and again, then stops on the value that asks for something impossible. A logic error can give the right answer for one value and the wrong answer for another, as the ride program does. One successful test proves only that the program works for that one input.
Testing can never reveal a syntax error. A program that breaks the rules of the language will not run at all, so there is nothing to test: the compiler or interpreter reports the mistakes before it starts. Those are found and fixed first, and testing only ever begins on a program that already runs.
Edexcel GCSE Computer Science · Errors in algorithms
Edexcel GCSE Computer Science · Syntax and Logical Errors
Edexcel GCSE Computer Science · Condition Controlled Iteration (While Loops)