Syntax, Runtime and Logic Errors

Syntax, runtime and logic errors classify software defects by whether code breaks language rules, fails during execution, or produces behaviour that violates its requirement. You use these categories to explain what went wrong before deciding how to repair it. The examples here use Python 3. The same distinctions apply across programming languages, although a particular mistake may be detected at a different stage in another language.

Recognize the three categories

A syntax error means the source code does not follow the language's grammar. For example, a Python if header needs a colon. A missing colon prevents the parser from accepting that statement. When Python reads an ordinary script containing this mistake, it rejects the script before executing its statements.


A runtime error occurs when execution reaches an operation that cannot complete under the current conditions. The code can follow the grammar perfectly and still attempt division by zero or access a list position that does not exist. In Python, these failures raise exceptions. An unhandled exception stops normal execution of the script.


A logic error means the implemented calculation or decision does not meet the stated requirement. The code can be grammatically valid and finish running while producing an incorrect result. A wrong formula, an incorrect condition or an omitted calculation can cause this type of defect.


Error category

What makes it this category?

Python example

Syntax

The code violates the language's grammar.

An if header has no colon.

Runtime

An operation fails when execution reaches it.

A calculation divides by zero.

Logic

The implemented behaviour violates the requirement.

An average calculation adds values but never divides by their count.



Separate execution from correctness

For a Python script, you can organize the distinction as three questions. Does Python accept the grammar? Can the executed operations complete for this input? Does the resulting behaviour meet the requirement?


Three connected boxes distinguish grammar checks, execution checks and result checks.


These questions are related, but passing an earlier check does not establish a later one. In particular, successful execution does not prove that the result is correct.


Example: A program must calculate the average of three readings: 8, 13 and 15. Their total is 36. The governing calculation is


average=sum of readingsnumber of readings=363=12.\text{average} = \frac{\text{sum of readings}}{\text{number of readings}} = \frac{36}{3} = 12.


If the program prints 36 because it omits the division, it has a logic error. Printing 36 is a valid operation, so Python has no reason to reject it. The requirement supplies the evidence that the result is wrong.

Account for inputs and hidden defects

A runtime failure may depend on the input. The expression total / count can execute successfully when count is 4 and fail when count is 0. The failure is a runtime error because the division becomes invalid during execution. The underlying defect might be an assumption that the count is always positive.


More than one defect can exist in a program. A syntax error can prevent you from reaching code that would raise a runtime error. After repairing both, you might discover that the formula still produces the wrong result. Describe each version and input precisely rather than assigning one permanent label to the whole program.



What to remember

  • Syntax errors break language grammar.
  • Runtime errors occur when an executed operation fails under the current conditions.
  • Logic errors make the implemented behaviour disagree with the requirement.
  • Earlier failures can hide later defects, and one successful run does not establish correctness.

Example: Sharing a meal delivery bill

This example distinguishes syntax, runtime and logic errors by checking grammar, execution and the required calculation. A student group orders meals costing 18 dollars per guest. Delivery costs 12 dollars for the whole order. The program must return each guest's equal share of the complete bill, with no other charges.


The first Python 3 version is:


Python
def share(meal_price, guests)
    return meal_price * guests + 12 / guests


The two inputs are the meal price per guest and the number of guests.


a) Python rejects this version before executing the function. Identify the error category and repair the function header.


A Python function header requires a colon after its closing parenthesis. The missing colon breaks the language's grammar, so this version has a syntax error.


The repaired header is:


Python
def share(meal_price, guests):


Adding the colon lets Python accept the header. It does not establish that the calculation inside the function is correct. The answer is a syntax error, repaired by adding the colon.


b) After repairing only the header, a user calls share(18, 0). Identify the error category and explain why no numeric share is returned.


Substituting the inputs makes the expression 18 * 0 + 12 / 0. Multiplication gives zero, but evaluating 12 / 0 attempts division by zero. Python raises a ZeroDivisionError before the expression can produce a numeric value.


The expression follows Python's grammar. Its failure occurs during execution for this particular input, so the answer is a runtime error caused by division by zero. A zero-guest order also has no meaningful per-guest share under this requirement.


c) With the repaired header, the group calls share(18, 8). Calculate the returned amount, identify the remaining error category, and give the correct amount per guest.


First calculate the meal subtotal. Eight meals at 18 dollars each cost ________. The expression then divides delivery by the number of guests, giving a delivery share of ________.


Python performs multiplication and division before addition, so the function evaluates


18(8)+128=144+1.50=145.50 dollars.18(8) + \frac{12}{8} = 144 + 1.50 = 145.50\ \text{dollars}.


That expression adds the entire meal subtotal to one guest's delivery share. It completes successfully, but it violates the requirement. The remaining defect is a logic error.


The complete bill is the meal subtotal plus delivery. Divide that entire amount by the number of guests:


share=18(8)+128=1568=19.50 dollars per guest.\text{share} = \frac{18(8) + 12}{8} = \frac{156}{8} = 19.50\ \text{dollars per guest}.


The corrected return statement is:


Python
return (meal_price * guests + 12) / guests


The function returns 145.50 dollars, but the required share is 19.50 dollars per guest. As a check, eight correct shares total 156 dollars, exactly covering the bill. The parentheses determine which amount is shared.

A student runs the following Python 3 script to display a message for a passing score. Which classification best describes its defect?


Python
score = 72
if score >= 50
    print("Pass")
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Match each programming term on the left with the description on the right that defines it. Each description is used exactly once, so read all of them before you match.

Match each term
A.

A failure that happens while a grammatically valid program is executing, such as dividing by zero, and stops it

B.

Code that breaks the grammar of the language, so the program is rejected before any of its lines run

C.

The report Python prints when a program crashes, naming the exception and the line where it occurred

D.

A loop or index boundary placed one step too far or one step too short

E.

A program that runs to completion without any message but produces a result different from the one intended

Runtime error

Off-by-one error

Logic error

Traceback

Syntax error

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An error category describes whether a defect breaks grammar, fails during execution, or violates the required result. Classify each independent Python 3 snippet as syntax, runtime or logic. Enter only one of those three words in each cell.


Snippet A must display the word Ready.


Python
print("Ready"


Snippet B must display the final value in the two-item list. Python list positions start at 0.


Python
values = [4, 9]
print(values[2])


Snippet C must display the total price, in dollars, of six notebooks costing 3 dollars each.


Python
print(6 + 3)
SnippetError category
A
B
C

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice