teach-ict.com logo

THE education site for computer science and ICT

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 are some very common logic errors.

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 33:              END WHILE

spotting logic errors

Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: What are some examples of logic errors in programming?