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

Look at the snippet of pseudocode below:

 

	Line 30:	SET mynumber TO 1
	Line 31:	WHILE mynumber <> 2  
	Line 32:		SEND mynumber TO DISPLAY
	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. This is an example of a logic error. To fix it, just add a way for the loop to end:

 

	Line 30:	SET mynumber TO 1
	Line 31:	WHILE mynumber <> 2  
	Line 32:		SEND mynumber TO DISPLAY
	Line 33:		SET mynumber TO mynumber + 1
	Line 34:	END WHILE

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?