Logic error
Encyclopedia
In computer programming
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

, a logic error (sometimes called a semantic error) is a bug
Software bug
A software bug is the common term used to describe an error, flaw, mistake, failure, or fault in a computer program or system that produces an incorrect or unexpected result, or causes it to behave in unintended ways. Most bugs arise from mistakes and errors made by people in either a program's...

 in a program that causes it to operate incorrectly, but not to terminate abnormally (or crash
Crash (computing)
A crash in computing is a condition where a computer or a program, either an application or part of the operating system, ceases to function properly, often exiting after encountering errors. Often the offending program may appear to freeze or hang until a crash reporting service documents...

). A logic error produces unintended or undesired output or other behavior, although it may not immediately be recognized as such.

Logic errors occur in both compiled
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 and interpreted
Interpreter (computing)
In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language...

 languages. Unlike a program with a syntax error
Syntax error
In computer science, a syntax error refers to an error in the syntax of a sequence of characters or tokens that is intended to be written in a particular programming language....

, a program with a logic error is a valid program in the language, though it does not behave as intended.

Common causes

The mistake could be the logical error in a statement
Statement (programming)
In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program written in such a language is formed by a sequence of one or more statements. A statement will have internal components .Many languages In computer programming...

 (for example, a wrong or incorrect formula), an error in an algorithm
Algorithm
In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning...

, or even the wrong algorithm selected.

Debugging logic errors

One of the ways to find these type of errors is to output the program's variables
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

 to a file or on the screen in order to define the error's location in code. Although this will not work in all cases, for example when calling the wrong subroutine
Subroutine
In computer science, a subroutine is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code....

, it is the easiest way to find the problem if the program uses the incorrect results of a bad mathematical calculation
Algorithm
In mathematics and computer science, an algorithm is an effective method expressed as a finite list of well-defined instructions for calculating a function. Algorithms are used for calculation, data processing, and automated reasoning...

.

Examples

This example in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 contains a logic error. After 'scanf
Scanf
Scanf format string refers to a control parameter used by a class of functions typically associated with some types of programming languages. The format string specifies a method for reading a string into an arbitrary number of varied data type parameter...

', 'money_in_store' is checked instead of 'money'.
  1. include


int money, money_in_store;

int main
{
do
{
printf("Enter amount of money to give to store (0 to exit): ");
scanf("%d", &money);
if (money_in_store

0) // Should be 'if (money

0)'
{
printf("%d money on exit\n", money_in_store);
exit(0);
}
money_in_store += money;
}
while(1);

return 0;
}
The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK