teach-ict.com logo

THE education site for computer science and ICT

2. Errors in logic

 

Logic errors break the flow of a program

 

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 != 10  
                 Line 32:              PRINT mynumber
                 Line 33:            ENDWHILE

The mynumber variable is set to 1 in line 30, and nothing ever changes it - so the code loops around forever.

The code below fixes this, by adding a line to change the value of mynumber with every loop until the condition is met.

 

                 Line 30:           mynumber = 1
                 Line 31:           WHILE mynumber != 10  
                 Line 32:              PRINT mynumber
                 Line 33:              mynumber = mynumber + 1
                 Line 33:           ENDWHILE

the kernel

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?