Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Logic error
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|A bug in a program that causes it to operate incorrectly, but not to terminate abnormally}} {{for|an error in logical reasoning|logical fallacy}} {{More citations needed|date=March 2025}} In [[computer programming]], a '''logic error''' is a [[Software bug|bug]] in a program that causes it to operate incorrectly, but not to terminate abnormally (or [[crash (computing)|crash]]).<ref>{{cite book |last1=Breedlove |first1=Todd W. |last2=Albert |first2=Randal L. |title=C++: An Active Learning Approach |date=22 October 2010 |publisher=Jones & Bartlett Publishers |isbn=978-1-4496-1283-2 |page=44 |url=https://books.google.com/books?id=FgnnQ8Gqx1wC&pg=PA44 |access-date=20 March 2025 |language=en}}</ref> 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 [[Compiler|compiled]] and [[Interpreter (computing)|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 program analysis|static analysis]] may sometimes spot them. ==Debugging logic errors== One of the ways to find this type of error is to put out the program's [[Variable (programming)|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 [[Algorithm|mathematical calculation]]. ==Examples== This example function in [[C (programming language)|C]] to calculate the [[Arithmetic mean|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 in C|operator precedence]] (division is evaluated before addition). <syntaxhighlight lang="c"> float average(float a, float b) { return a + b / 2; // should be (a + b) / 2 } </syntaxhighlight> ==See also== *[[Syntax error]] *[[Off-by-one error]] ==References== {{Reflist}} [[Category:Computer errors]] [[Category:Programming language theory]] {{compu-prog-stub}} [[bg:Логическа грешка]] [[de:Logischer Fehler]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite book
(
edit
)
Template:Compu-prog-stub
(
edit
)
Template:For
(
edit
)
Template:More citations needed
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)