Logic Error

A logic error (or logical error) is a mistake in a program's source code that results in incorrect or unexpected behavior. It is a type of runtime error that may simply produce the wrong output or may cause a program to crash while running.

Many different types of programming mistakes can cause logic errors. For example, assigning a value to the wrong variable may cause a series of unexpected program errors. Multiplying two numbers instead of adding them together may also produce unwanted results. Even small typos that do not produce syntax errors may cause logic errors. In the PHP code example below, the if statement may cause a logic error since the single equal sign (=) should be a double equal sign (==).

Incorrect: if ($i=1) { ... }

Correct: if ($i==1) { ... }

In PHP, "==" means "is equal to," while "=" means "becomes." Therefore, the incorrect if statement always returns TRUE, since assigning 1 to the variable $i returns a TRUE value. In the correct code, the if statement only returns TRUE if $i is equal to 1. However, since the syntax of the incorrect code is acceptable, it will not produce a syntax error and the code will compile successfully. The logic error might only be noticed during runtime. Because logic errors are often hidden in the source code, they are typically harder to find and debug than syntax errors.

Updated April 27, 2012 by Per C.

quizTest Your Knowledge

Which type of malware replicates itself?

A
Spyware
0%
B
Adware
0%
C
Virus
0%
D
Trojan horse
0%
Correct! Incorrect!     View the Virus definition.
More Quizzes →

The Tech Terms Computer Dictionary

The definition of Logic Error on this page is an original definition written by the TechTerms.com team. If you would like to reference this page or cite this definition, please use the green citation links above.

The goal of TechTerms.com is to explain computer terminology in a way that is easy to understand. We strive for simplicity and accuracy with every definition we publish. If you have feedback about this definition or would like to suggest a new technical term, please contact us.

Sign up for the free TechTerms Newsletter

How often would you like to receive an email?

You can unsubscribe or change your frequency setting at any time using the links available in each email.

Questions? Please contact us.