Jump to content

Logic error

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 92.42.56.4 (talk) at 13:37, 30 September 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

One of the ways to find this type of error is to put out the program's variables 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, 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 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 (division is evaluated before addition).

float average(float a, float b)
{
    return a + b / 2;  // should be (a + b) / 2
}

See also