Debugging, Testing and Logical Reasoning
Types of Error
- A syntax error breaks the rules of the programming language, such as a spelling mistake in a command. The program will not run.
- A logic error means the program runs but gives the wrong result, for example adding when it should subtract.
Dry Runs and Tracing
A dry run means working through a program by hand, line by line, and writing down the value of each variable. A trace table helps with this.
| Step | Instruction | x |
|---|---|---|
| 1 | set x to 5 | 5 |
| 2 | add 3 to x | 8 |
| 3 | double x | 16 |
Testing
Testing means running the program with different inputs to see if it behaves correctly. Test normal cases and unusual ones.
Searching: Finding a Name in a List
A linear search checks each item in turn until it finds the target. It works on any list but can be slow.
A binary search works on a sorted list. Look at the middle, decide whether the target is higher or lower, throw away half, and repeat. To find a number between 1 and 100, you need at most 7 guesses, because halving 100 repeatedly gets to 1 in 7 steps.
Sorting
Sorting puts items in order. To sort 5, 2, 9, 1 into ascending order, one method is to repeatedly compare neighbours and swap them if they are in the wrong order. The result is 1, 2, 5, 9.
Logical Reasoning
Computers use logic:
- A AND B is true only when both are true.
- A OR B is true when at least one is true.
- NOT A is the opposite of A.
You can explain how an algorithm works by following its steps and saying why each step is needed.
Curriculum link: National Curriculum in England: Computing programmes of study (DfE) — Key Stage 2: use logical reasoning to explain how some simple algorithms work and to detect and correct errors in algorithms and programs