Errors in algorithms
A logic error is a mistake in the thinking behind a program rather than in the way it is written. The code is syntactically correct, so the computer accepts every line and runs the program right through to the end — but the result is not the one you wanted. The computer has done exactly what it was told to do; the fault is in what you told it to do.
Because the computer simply follows your instructions, no error message usually appears. The program finishes quietly with the wrong answer, the wrong message, or the wrong value stored in a variable.
> where >= is needed, or + instead of -.count when it should have gone into total.Conditions are where logic errors hide most often, because they control both selection and loops. A loop condition that can never become true makes the loop repeat forever, which is a logic error too.
No compiler can find a logic error for you: every line is valid, so there is nothing for it to report. That is the difference from a syntax error, which breaks the grammar of the language and stops the program running at all.
In this program, anyone aged 12 or under should pay the child price of £6, and everyone else should pay £12.
age = int(input())
price = 12
if age >= 13: # logic error: the condition is back to front
price = 6
print("Ticket price: " + str(price))Output when the user types 12:
12
Ticket price: 12
The rule is written back to front, so adults get the child price while children pay full price. Changing the condition to age < 13 puts it right.
Edexcel GCSE Computer Science · Errors in algorithms
Edexcel GCSE Computer Science · Syntax and Logical Errors
Edexcel GCSE Computer Science · Arithmetic Operators
Edexcel GCSE Computer Science · Relational Operators