Debugging methods
2. Errors in logic
Look at the following sentence:
My pencil just rode a bike to London
While it is a grammatical sentence, it makes no sense. Pencils don't ride bikes. Somewhere, the programmer writing this sentence has made a logical error. Only the programmer can spot these logic errors because the computer will just do exactly as it has been told.
Here is a very common logic error.
Infinite loops
This pseudocode has a logic error
Line 30: mynumber = 1
Line 31: WHILE mynumber != 2
Line 32: PRINT mynumber
Line 33: END WHILE
The mynumber variable is set to 1 in line 30, and nothing ever changes it - so the code loops around forever. The code below guarantees the while loop can end because of the increment mynumber command at line 33
Line 30: mynumber = 1
Line 31: WHILE mynumber != 2
Line 32: PRINT mynumber
Line 33: INCREMENT mynumber
Line 34: END WHILE