logic error
   HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, a logic error is a bug in a program that causes it to operate incorrectly, but not to terminate abnormally (or crash). A logic error produces unintended or undesired output or other behaviour, although it may not immediately be recognized as such. Logic errors occur in both
compiled In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
and interpreted languages. Unlike a program with a
syntax error In computer science, a syntax error is an error in the syntax of a sequence of characters that is intended to be written in a particular programming language. For compiled languages, syntax errors are detected at compile-time. A program will no ...
, a program with a logic error is a valid program in the language, though it does not behave as intended. Often the only clue to the existence of logic errors is the production of wrong solutions, though
static analysis Static analysis, static projection, or static scoring is a simplified analysis wherein the effect of an immediate change to a system is calculated without regard to the longer-term response of the system to that change. If the short-term effect i ...
may sometimes spot them.


Debugging logic errors

One of the ways to find this type of error is to put out the program's
variables Variable may refer to: Computer science * Variable (computer science), a symbolic name associated with a value and whose associated value may be changed Mathematics * Variable (mathematics), a symbol that represents a quantity in a mathemat ...
to a file or on the screen in order to determine the error's location in code. Although this will not work in all cases, for example when calling the wrong
subroutine In computer programming, a function (also procedure, method, subroutine, routine, or subprogram) is a callable unit of software logic that has a well-defined interface and behavior and can be invoked multiple times. Callable units provide a ...
, it is the easiest way to find the problem if the program uses the incorrect results of a bad mathematical calculation.


Examples

This example function in C to calculate the
average In colloquial, ordinary language, an average is a single number or value that best represents a set of data. The type of average taken as most typically representative of a list of numbers is the arithmetic mean the sum of the numbers divided by ...
of two numbers contains a logic error. It is missing parentheses in the calculation, so it compiles and runs but does not give the expected answer due to
operator precedence In mathematics and computer programming, the order of operations is a collection of rules that reflect conventions about which operations to perform first in order to evaluate a given mathematical expression. These rules are formalized with a ...
(division is evaluated before addition). float average(float a, float b)


See also

*
Syntax error In computer science, a syntax error is an error in the syntax of a sequence of characters that is intended to be written in a particular programming language. For compiled languages, syntax errors are detected at compile-time. A program will no ...
*
Off-by-one error An off-by-one error or off-by-one bug (known by acronyms OBOE, OBOB, OBO and OB1) is a logic error that involves a number that differs from its intended value by 1. An off-by-one error can sometimes appear in a mathematics, mathematical context. ...


References

Computer errors Programming language theory {{compu-prog-stub bg:Логическа грешка de:Logischer Fehler