Menu
Coddy logo textTech

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.

By Kevin Spektor, Co-founder & CTO

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 / 2 divides only b, as in the average above.
  • The wrong comparison. < where <= was needed, or and where or was needed. Conditions are boolean expressions, and one wrong operator flips the result for some inputs.
  • Conditions in the wrong order. In an if and elif chain, the first true condition wins, so a broad test placed first hides the narrower ones below it.
  • The wrong variable. Updating total inside a loop but printing subtotal.
  • Assumptions about numbers. 0.1 + 0.2 == 0.3 is False in Python, Java and JavaScript, because floating point numbers are approximations. 7 // 2 is 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 errorRuntime errorLogic error
When it appearsBefore the program runsWhile it runsWhile it runs, or never noticed
What detects itThe parser or compilerThe runtime or the operating systemA person or a test
Does the program run?NoUntil the failing lineYes, to the end
Error messageYes, with a line numberYes, with a tracebackNone
Python exampleif x > 3 without the colon10 / 0a + b / 2 for an average
How you find itRead the messageRead the tracebackCompare 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

  1. 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.
  2. Print the values in between. Printing score and the branch taken, or the loop counter on every pass, shows the exact step where reality departs from your plan.
  3. Step through with a debugger. Setting a breakpoint and running one line at a time does the same without editing the code.
  4. Test the edges. Try 0, 1, the largest value, an empty list, and the exact threshold (60 and 90 in the grading example).
  5. 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?
A syntax error breaks the grammar of the language, so the program is rejected before it runs and you get a message with a line number. A logic error follows the grammar perfectly and runs to the end, but the result is wrong, and there is no message at all. You find a syntax error by reading the message; you find a logic error by testing the output.
What is a logic error in Python?
It is Python code that runs without a traceback but gives the wrong answer. Typical examples are 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.
What are the 5 types of errors in programming?
A common list of five is syntax errors, semantic (type) errors, linker errors, runtime errors and logic errors. The first three are found while the program is being built, runtime errors appear while it runs, and logic errors show up only as wrong results. Many courses teach just three: syntax, runtime and logic.
Is a logic error a bug?
Yes. A bug is any defect that makes a program behave differently from what was intended, and logic errors are the most common kind. Crashes and error messages are bugs too, but they announce themselves, while a logic error has to be noticed by a person or a test.
Why are logic errors hard to find?
Because nothing points at them. There is no line number and no message, and the wrong result may look plausible, like an average of 203 for three test scores that nobody double checks. They are found by comparing the output with an answer you already know, which is why tests with known results matter.
Coddy programming languages illustration

Learn to code with Coddy

GET STARTED