Identify and categorise errors

Identifying and Categorising Errors

Being able to identify an error means spotting that something is wrong and locating it. Being able to categorise it means labelling it correctly as either a syntax error or a logic error - and explaining why. This is a separate skill from simply knowing the definitions: you need to look at code, decide what type of error is present, and justify your answer.

A reliable two-step approach is:

  1. Ask: does this code follow the rules of the language? If a keyword is missing, a bracket is unclosed, or a statement is missing its terminator, it is a syntax error. The program cannot run.
  2. Ask: does this code produce the correct result for all inputs? If the code runs but the output is wrong for at least one input - especially a boundary value - it is a logic error.

The tabs below each show a short algorithm or program containing one or more errors. For each, the error is identified, categorised, and the reasoning is explained. Work through each tab as a model for how to approach an exam question.

Categorising Errors in Practice

Syntax Error - Missing Structure Keyword

Step 1 - Identify: the loop body runs but there is no closing keyword to end the loop. The structure is incomplete.

Step 2 - Categorise: Syntax error. A language rule has been broken: all loop structures must be closed. The interpreter or compiler cannot process the code and the program is rejected before it runs. No output is ever produced.

# Python - syntax error: indentation missing after for
total = 0
for i in range(1, 6):
total = total + i       # No indentation - IndentationError (syntax)
print(total)

Identification: the line total = total + i is not indented, so Python does not treat it as part of the loop body. Category: syntax error - Python enforces indentation as a language rule. Detected before execution.

' VB.NET - syntax error: Next is missing
Dim total As Integer = 0
For i As Integer = 1 To 5
    total = total + i
' Next is missing here - compile error
Console.WriteLine(total)

Identification: Next is missing to close the For loop. Category: syntax error - VB.NET requires every For to be closed with Next. Detected at compile time.

// C# - syntax error: closing brace missing
int total = 0;
for (int i = 1; i <= 5; i++)
{
    total = total + i;
// Closing brace missing - compile error
Console.WriteLine(total);

Identification: the closing } for the for loop is absent. Category: syntax error - C# requires all opening braces to be matched. Detected at compile time.

Logic Error - Wrong Comparison Operator

Step 1 - Identify: the program runs and produces output. Testing with values clearly inside the range works correctly. Testing with the exact boundary value reveals the fault: the boundary is incorrectly excluded.

Step 2 - Categorise: Logic error. All language rules are followed - the syntax is valid. The flaw is in the programmer's reasoning: > was written when >= was needed. Only boundary testing exposes it.

# Python - logic error: operator excludes the lower boundary
# Intended: valid if age is 18 or above
age = int(input("Enter age: "))
if age > 18:            # Logic error: should be >=
    print("Eligible")
else:
    print("Not eligible")
# Test age=19: correct. Test age=18: wrong output.
' VB.NET - logic error: operator excludes the lower boundary
' Intended: valid if age is 18 or above
Dim age As Integer = CInt(Console.ReadLine())
If age > 18 Then        ' Logic error: should be >=
    Console.WriteLine("Eligible")
Else
    Console.WriteLine("Not eligible")
End If
' Test age=19: correct. Test age=18: wrong output.
// C# - logic error: operator excludes the lower boundary
// Intended: valid if age is 18 or above
int age = int.Parse(Console.ReadLine());
if (age > 18)           // Logic error: should be >=
{
    Console.WriteLine("Eligible");
}
else
{
    Console.WriteLine("Not eligible");
}
// Test age=19: correct. Test age=18: wrong output.

Mixed - Two Errors, One of Each Type

When a program contains both a syntax error and a logic error, the syntax error must be corrected first - the program cannot run until then. Once it runs, the logic error becomes visible through testing. The two errors are independent and must be identified and categorised separately.

# Python - two errors
total = 0
for i in range(1, 6):
    total = total + i
print(total / 4)        # Syntax is valid but divides by 4, not 5 (logic error)
                        # Also: range(1,6) gives 1,2,3,4,5 - loop count is correct
                        # Error 1 (syntax): if the colon after range(1,6) were missing
                        # Error 2 (logic): divides by 4 instead of 5
' VB.NET - two errors
Dim total As Integer = 0
For i As Integer = 1 To 5
    total = total + i
' Next missing here (syntax error - compile time)
Console.WriteLine(total / 4)   ' Should be total / 5 (logic error - run time)
// C# - two errors
int total = 0;
for (int i = 1; i <= 5; i++)
{
    total = total + i
}                               // Closing brace present but semicolon missing (syntax error)
Console.WriteLine(total / 4);  // Should be total / 5 (logic error)

 Key Takeaways

  • To identify an error: find the specific line or expression that is wrong. To categorise it: label it as syntax or logic and explain your reasoning.
  • If the program cannot run, the error is a syntax error. If it runs but gives wrong results, the error is a logic error.
  • Logic errors can only be found by testing - especially with boundary values. A logic error that only affects the boundary value will pass all tests that avoid it.
  • A program can contain both error types simultaneously. Fix syntax errors first (the program cannot run until they are resolved), then use testing to find any remaining logic errors.
  • In an exam, always state the category and justify it - naming the error type without explanation is not a full answer.