Hubbry Logo
search button
Sign in
Logic error
Logic error
Comunity Hub
History
arrow-down
starMore
arrow-down
bob

Bob

Have a question related to this hub?

bob

Alice

Got something to say related to this hub?
Share it here.

#general is a chat channel to discuss anything related to the hub.
Hubbry Logo
search button
Sign in
Logic error
Community hub for the Wikipedia article
logoWikipedian hub
Welcome to the community hub built on top of the Logic error Wikipedia article. Here, you can discuss, collect, and organize anything related to Logic error. The purpose of the hub is to connect people, f...
Add your contribution
Logic error

In computer programming, a logic error is a bug in a program that causes it to operate incorrectly, but not to terminate abnormally (or crash).[1] 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 and interpreted languages. Unlike a program with a syntax error, 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 may sometimes spot them.

Debugging logic errors

[edit]

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

[edit]

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

[edit]

References

[edit]
  1. ^ Breedlove, Todd W.; Albert, Randal L. (22 October 2010). C++: An Active Learning Approach. Jones & Bartlett Publishers. p. 44. ISBN 978-1-4496-1283-2. Retrieved 20 March 2025.