DoAssignment study guide

B2.6 · Distinguish syntax, logic and run-time errors

Learn to distinguish syntax, logic and run-time errors through clear examples and targeted practice.

Ontario Grade 10 Computer Science

Introduction to Programming

Ontario Grade 10 Computer Studies — study topic B2.6

A program can fail in different ways. It might break a rule of the programming language, carry out the wrong steps, or encounter a problem while it is running. These are syntax, logic, and run-time errors. Learning to distinguish them helps you describe a problem clearly and choose a sensible next check. We will use a simple school scenario: a program asks for the number of minutes someone read and reports whether the reading goal was reached. Input is the information provided to the program, process is the work the program does with it, and output is the result it presents. The examples use Python 3; classroom languages can differ in their rules and punctuation.

What you will learn

  • Distinguish syntax errors, logic errors, and run-time errors.
  • Use a program’s input, process, and output to describe what it is meant to do.
  • Identify what to check when a program does not work as intended.

1. Start with the program’s purpose

Before deciding what kind of error you see, state what the program is supposed to do. For the reading example, the input is a number of minutes. The process compares that number with a goal of 20 minutes. The output tells the reader whether the goal was reached. This input–process–output description gives you a simple way to check what the code actually does.
A useful first question is, “Can the program start?” If the language cannot understand the code as written, there is a syntax error. If the program starts but gives the wrong answer, there may be a logic error. If it starts but stops unexpectedly while handling particular input or doing a particular task, there may be a run-time error.
  • Input is the information a program receives.
  • Process is the work the program carries out.
  • Output is the information the program presents.
  • The intended result is the reference point for identifying a logic error.

2. Syntax errors: code that breaks language rules

A syntax error occurs when code does not follow the rules of the programming language. Those rules include how instructions are written and arranged. For instance, Python uses a colon at the end of an if condition. If the colon is missing, Python cannot properly read that instruction, so the program cannot proceed as intended.
When a syntax error appears, look closely at the line identified by the language and nearby lines. Check punctuation, spelling of language keywords, and indentation. A message may point to where the language noticed a problem; the actual cause can sometimes be just before that point. Fix the code rule that is broken, then review the line again.
A syntax error is not the same as an answer that is merely wrong. The program has not successfully carried out the intended instructions. Different classroom languages have different syntax rules, so a line accepted in one language may not be accepted in another.
  • Syntax concerns whether the code is written in a form the language accepts.
  • Check punctuation, spelling, and indentation when looking for a syntax error.
  • Do not call every incorrect result a syntax error.

3. Logic errors: code that does the wrong thing

A logic error occurs when the program can run but its instructions do not correctly achieve the intended purpose. The language accepts the code, and the program may produce output, but the output is wrong for the task.
Suppose the reading goal is 20 minutes, and reaching exactly 20 counts as success. The condition must include equality: a value of 20 should receive the success message. If the condition checks only for a value greater than 20, the program will report that exactly 20 minutes is not enough. That is a logic error: the code is readable, but the rule in the program does not match the stated goal.
To check a possible logic error, compare the expected result with the result the written instructions would produce. Try an ordinary input and a boundary input, such as exactly the goal. These are checks you can reason through; they do not require claiming that the code has been run.
  • A logic error is about the meaning or steps of a program, not its written form.
  • Compare the program’s instructions with the task it is meant to complete.
  • Test the stated boundary, especially when equality should count.

4. Run-time errors: a problem while the program runs

A run-time error occurs after a program has been accepted well enough to start, when something goes wrong during its operation. For example, a program may ask the user to enter a whole number and then try to treat the response as a whole number. If the response is the word “many,” that conversion cannot be completed, and the program may stop with an error.
In this kind of situation, the code’s intended steps can make sense, but the actual input does not fit what the program is prepared to handle. This differs from a syntax error, which prevents the language from understanding the code, and from a logic error, which gives an incorrect result because the instructions do not match the task.
When you identify a run-time error, note what the program was doing and what input or situation led to the failure. A good explanation names the category and the reason: for example, “The program stopped while trying to convert a word into a whole number.”
  • A run-time error happens during program operation.
  • The program may work for some inputs but fail for another.
  • Describe the action and situation that caused it to stop.

Three kinds of programming error

Error typeWhat goes wrongReading example
SyntaxCode breaks a language writing rule.The if line is missing its colon.
LogicThe instructions do not match the intended task.Exactly 20 minutes is treated as below the goal.
Run-timeA problem occurs while the program operates.The word “many” cannot be converted to a whole number.

Worked example

Example 1: A missing colon

The program should ask for minutes and then decide whether the reader reached the 20-minute goal. The if line is missing punctuation. What kind of error is this, and what needs attention?

Python 3

minutes = int(input("Minutes read: "))
if minutes >= 20
    print("Goal reached")
  1. Inspect the code form
    In Python 3, an if condition needs a colon before the indented instruction that follows. The displayed line omits it, so the code breaks a Python writing rule.
  2. Name the error
    This is a syntax error. The issue is not whether 20 is the right goal; the program cannot correctly read the written if instruction until the punctuation is fixed.
  3. Make the correction
    Add the missing colon. The following indented line is the output instruction for the condition.
Answer: It is a syntax error: the if line is missing the colon required by Python 3.
Check: This example identifies a Python syntax rule. Another classroom language may use different punctuation.

Worked example

Example 2: The equality boundary

The goal is to report success when someone reads 20 minutes or more. The program uses a condition that checks whether the minutes are greater than 20. What happens for an input of 20?

Python 3

minutes = int(input("Minutes read: "))
if minutes > 20:
    print("Goal reached")
else:
    print("Keep reading")
  1. State the intended rule
    The goal is reached at 20 minutes as well as at any larger amount. Therefore, exactly 20 should lead to the success message.
  2. Trace the given input
    For an input of 20, the condition “greater than 20” is false. The program therefore prints “Keep reading,” even though the stated goal has been reached.
  3. Classify the problem
    The code is written in a form Python can read, but its condition does not match the goal. This is a logic error. Use a condition that includes equality.
Answer: For input 20, the program prints “Keep reading.” This is a logic error because the stated goal counts exactly 20 as success.
Check: The corrected comparison is greater than or equal to 20, so the equality boundary passes.

Worked example

Example 3: A response that cannot become a number

A program asks for minutes and converts the response to a whole number. A user enters the word “many.” What kind of error can occur, and why?

Python 3

minutes = int(input("Minutes read: "))
print("Minutes entered:", minutes)
  1. Follow the input and process
    The input is the text the user types. The process tries to convert that text into a whole number so it can compare the minutes with the goal.
  2. Check the specific response
    The word “many” is not a whole-number entry. The conversion cannot be completed, so Python may stop with an error while the program is running.
  3. Distinguish the category
    This is a run-time error, not a syntax error: the code’s form is readable. It is not the logic error shown in Example 2: the immediate problem is that this input cannot be converted as requested.
Answer: A run-time error can occur when the program tries to convert “many” into a whole number.
Check: A response such as 15 can be converted to a whole number; “many” cannot.

Common mistakes and how to avoid them

Calling any wrong output a syntax error.
Correction: If the program can run but produces an incorrect result, check its logic. Syntax is about whether the code follows the language’s writing rules.
Calling an error caused by unsuitable input a logic error automatically.
Correction: If the program stops while handling that input, consider a run-time error. Describe what the program was doing when it stopped.
Checking only values above or below a goal.
Correction: Check the exact boundary too. If reaching the goal includes equality, try the value equal to the goal.

Lesson summary

  • Syntax errors break the writing rules of a programming language.
  • Logic errors occur when acceptable code does not do what the task requires.
  • Run-time errors occur while a program is operating, often when a particular input or situation cannot be handled.
  • Use the program’s input, process, and output, together with its intended purpose, to help tell the three categories apart.

Check your understanding

Question 1

A Python 3 if line is missing its required colon, so the program cannot proceed as written. Which error is this?
  1. Syntax error
  2. Logic error
  3. Run-time error
  4. correctIndex": 0,
    "explanation": "The line breaks a Python writing rule, so this is a syntax error."
    },
    {
    "question": "A program runs, but it says the 20-minute goal was not reached when the goal is 20 minutes. Which error best describes this?",
    "options": [
    "Syntax error",
    "Logic error",
    "Run-time error"
    ],
    "correctIndex": 1,
    "explanation": "The program can run, but its result does not match the intended rule."
    },
    {
    "question": "A program stops when it tries to convert the typed word “many” into a whole number. Which error best describes this?",
    "options": [
    "Syntax error",
    "Logic error",
    "Run-time error"
    ],
    "correctIndex": 2,
    "explanation": "The failure occurs during operation when the input cannot be converted as requested."
    }
    ],
    "glossary": [
    {
    "term": "Input",
    "definition": "Information provided to a program."
    },
    {
Show answer and explanation
Syntax error
The line breaks a Python writing rule, so this is a syntax error.

Question 2

A program runs, but it says the 20-minute goal was not reached when the goal is 20 minutes. Which error best describes this?
  1. Syntax error
  2. Logic error
  3. Run-time error
  4. correctIndex": 1,
    "explanation": "The program can run, but its result does not match the intended rule."
    },
    {
    "question": "A program stops when it tries to convert the typed word “many” into a whole number. Which error best describes this?",
    "options": [
    "Syntax error",
    "Logic error",
    "Run-time error"
    ],
    "correctIndex": 2,
    "explanation": "The failure occurs during operation when the input cannot be converted as requested."
    }
    ],
    "glossary": [
    {
    "term": "Input",
    "definition": "Information provided to a program."
    },
    {
    "term": "Process",
    "definition": "The work a program does with its input."
    },
    {
    "term": "Output",
    "definition": "Information a program presents as a result."
    },
    {
    "term": "Syntax error",
    "definition": "An error caused by code that does not follow the writing rules of its programming language."
    },
    {
    "term":
Show answer and explanation
Logic error
The program can run, but its result does not match the intended rule.

Question 3

A program stops when it tries to convert the typed word “many” into a whole number. Which error best describes this?
  1. Syntax error
  2. Logic error
  3. Run-time error
  4. correctIndex": 2,
    "explanation": "The failure occurs during operation when the input cannot be converted as requested."
    }
    ],
    "glossary": [
    {
    "term": "Input",
    "definition": "Information provided to a program."
    },
    {
    "term": "Process",
    "definition": "The work a program does with its input."
    },
    {
    "term": "Output",
    "definition": "Information a program presents as a result."
    },
    {
    "term": "Syntax error",
    "definition": "An error caused by code that does not follow the writing rules of its programming language."
    },
    {
    "term": "Logic error",
    "definition": "An error in the program’s instructions that causes it to do something other than the intended task."
    },
    {
    "term": "Run-time error",
    "definition": "An error that happens while a program is operating."
    }
    ]
    }
Show answer and explanation
Run-time error
The failure occurs during operation when the input cannot be converted as requested.

Key terms

Input
Information provided to a program.
Process
The work a program does with its input.
Output
Information a program presents as a result.
Syntax error
An error caused by code that does not follow the writing rules of its programming language.
Logic error
An error in the program’s instructions that causes it to do something other than the intended task.
Run-time error
An error that happens while a program is operating.

Continue through ICS2O

View the complete ICS2O Ontario Grade 10 Computer Science curriculum and lessons

Tutor support for B2.6 · Distinguish syntax, logic and run-time errors

Working on B2.6 · Distinguish syntax, logic and run-time errors in ICS2O? Bring the question and your own attempt. In a live session, you can trace a small example and find the first step where the output differs from what you expected. Ask about the first step that does not make sense, then try a similar question yourself.

Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.

برای مبحث B2.6 · تمایز خطای نوشتاری، منطقی و زمان اجرا در درس ICS2O، سؤال و تلاش خود را همراه بیاورید. در جلسه می‌توانید یک مثال کوچک را دنبال کنید و نخستین گامی را که خروجی با انتظار شما فرق دارد پیدا کنید. راهنمای معلم برنامه‌نویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقه‌ای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی می‌شود.

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 10 Computer Science (ICS2O), expectation B2.6. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question