What Is a Logic Error?
A logic error is a mistake in a program's reasoning that makes it produce the wrong result without crashing or showing any error message. The code is valid and every line runs, but it does not do what the programmer intended.
Updated September 24, 2026
Three test scores, 80, 90 and 100, have an average of 90. This program runs without a single complaint and prints something else:
Average: 203.33333333333334
Python did exactly what the line says. Division happens before addition, so only the last score was divided by 3, and the result is 80 + 90 + 33.33. The programmer meant (scores[0] + scores[1] + scores[2]) / 3. Add the parentheses and the program prints Average: 90.0.
Why no error message appears
A computer checks whether code is valid, not whether it is correct. The parser checks the grammar, which catches a syntax error. The runtime checks each operation, which catches a runtime error such as dividing by zero. Adding three numbers and dividing one of them by 3 is valid grammar and a legal operation, so both checks pass.
Only the person who wrote the program knows it was supposed to calculate an average. That intention exists nowhere in the code, so no tool can compare the result against it. A logic error is the gap between what you told the computer to do and what you wanted it to do.
Common causes of logic errors
Most logic errors come from a short list of patterns.
- Off-by-one errors. A loop runs one time too many or too few.
range(1, 5)produces 1, 2, 3 and 4, because the end is excluded. See iteration. - Operator precedence.
a + b / 2divides onlyb, as in the average above. - The wrong comparison.
<where<=was needed, orandwhereorwas needed. Conditions are boolean expressions, and one wrong operator flips the result for some inputs. - Conditions in the wrong order. In an
ifandelifchain, the first true condition wins, so a broad test placed first hides the narrower ones below it. - The wrong variable. Updating
totalinside a loop but printingsubtotal. - Assumptions about numbers.
0.1 + 0.2 == 0.3isFalsein Python, Java and JavaScript, because floating point numbers are approximations.7 // 2is 3, not 3.5.
Here is the condition order mistake. A score of 95 should be an A:
D
D
F
95 is greater than or equal to 60, so the first branch wins and the elif is never reached. Put the highest threshold first and every result becomes right.
Types of errors in programming
Programming errors are usually sorted into three types by when they appear and what reports them.
| Syntax error | Runtime error | Logic error | |
|---|---|---|---|
| When it appears | Before the program runs | While it runs | While it runs, or never noticed |
| What detects it | The parser or compiler | The runtime or the operating system | A person or a test |
| Does the program run? | No | Until the failing line | Yes, to the end |
| Error message | Yes, with a line number | Yes, with a traceback | None |
| Python example | if x > 3 without the colon | 10 / 0 | a + b / 2 for an average |
| How you find it | Read the message | Read the traceback | Compare output with a known answer |
Some textbooks list more types, and most of them fit inside these three. A type error that the Java compiler rejects, such as incompatible types: String cannot be converted to int, is found at compile time like a syntax error, and some books call it a semantic error. A linker error happens in the last step of building a C or C++ program, when a function was declared but its code is nowhere to be found. Arithmetic, resource and file errors, such as dividing by zero, running out of memory or opening a missing file, are all runtime errors. The term "semantic error" is also used by some courses as another name for a logic error, so check which meaning a textbook uses.
How to find a logic error
- Test with an answer you already know. Work out the correct result by hand for a small input, then compare. Three scores are easier to check than three thousand.
- Print the values in between. Printing
scoreand the branch taken, or the loop counter on every pass, shows the exact step where reality departs from your plan. - Step through with a debugger. Setting a breakpoint and running one line at a time does the same without editing the code.
- Test the edges. Try 0, 1, the largest value, an empty list, and the exact threshold (60 and 90 in the grading example).
- Explain the code out loud. Describing each line to someone else, or to a rubber duck, forces you to say what the line does rather than what you assume it does.
An assert turns a known answer into an automatic check. It does nothing when the condition is true and stops the program when it is false:
Traceback (most recent call last):
File "main.py", line 10, in <module>
assert grade(95) == "A", "95 should be an A"
^^^^^^^^^^^^^^^^
AssertionError: 95 should be an A
A failing test converts a silent logic error into a loud one with a line number. That is the whole idea behind unit testing.
Logic errors in other languages
The patterns are the same everywhere, but each language has its own traps. In JavaScript, "5" + 3 is the string "53", while "5" - 3 is the number 2, so a value read from a form as text can add up wrong without any error. In C, if (x = 5) assigns 5 to x and is always true. Clang warns about it by default (using the result of an assignment as a condition without parentheses), but it still compiles and runs, so read compiler warnings as seriously as errors.
Where to go next
Compare this page with the syntax error and runtime error pages to see how each type is reported. Loops are where off-by-one errors live, so the iteration page and the Python guide to for loops are good next reads. To practice finding wrong results in real exercises, try the Python course.
Frequently Asked Questions
What is the difference between a syntax error and a logic error?
What is a logic error in Python?
range(1, 5) when you wanted 1 to 5 inclusive, a + b / 2 when you wanted (a + b) / 2, and if branches checked in the wrong order. Python cannot detect them, because it has no way to know what you meant.