Types of error
Types of Error in Programs
Not all errors in a program are the same. A program can fail in two fundamentally different ways: it may fail to run at all, or it may run but produce wrong results. This distinction matters because the two types of error - syntax errors and logic errors - occur at different stages, are detected differently, and require different strategies to avoid and fix.
| Error type | Definition | When detected | Does the program run? |
|---|---|---|---|
| Syntax error | A rule of the programming language has been broken | Before execution - at compile or parse time | No - the program is rejected before it runs |
| Logic error | The program runs but produces incorrect results due to flawed reasoning | During or after execution - only revealed by testing | Yes - but the output is wrong |
In pseudocode, syntax rules are informal - a pseudocode algorithm cannot truly "fail to run" in the way compiled code can. However, the same distinction applies: some errors are structural violations (equivalent to syntax errors) and some are logical flaws that would only become visible when values are traced through the algorithm.
Pseudocode syntax error (structural violation):
IF score > 50 ← ENDIF is missing - structure is incomplete
Pseudocode logic error (flawed reasoning):
IF score > 50 THEN ← uses > instead of >=
OUTPUT "Pass" ← score of 50 would give "Fail" - wrong
ELSE
OUTPUT "Fail"
ENDIF
Errors in Coded Programs
The tabs below show three scenarios: a syntax error caught before the program runs, a logic error that only surfaces when the program is tested with specific values, and a program containing both types simultaneously. For each, note when and how the error is detected, and the strategy most likely to catch it.
Syntax Error - Program Rejected Before Running
What has gone wrong: a rule of the language has been broken. The interpreter or compiler cannot make sense of the code and refuses to run it. The error is reported immediately, often with a line number.
Strategy: read the error message carefully - it usually identifies the line. IDEs (Integrated Development Environments) underline syntax errors in real time, similar to a spell-checker underlining a misspelt word.
# Python - syntax error example
# Error: missing colon at end of if statement
score = int(input("Enter score: "))
if score >= 50 # SyntaxError: expected ':'
print("Pass")
else:
print("Fail")
Python requires a colon (:) at the end of every if, else, for, while, def line. The missing colon is detected immediately and the program is rejected before any code runs.
' VB.NET - syntax error example
' Error: End If is missing - structure is incomplete
Dim score As Integer = CInt(Console.ReadLine())
If score >= 50 Then
Console.WriteLine("Pass")
Else
Console.WriteLine("Fail")
' End If missing here - compile error
VB.NET requires every If block to be closed with End If. The missing keyword is caught at compile time before the program can run.
// C# - syntax error example
// Error: missing semicolon after Console.WriteLine
int score = int.Parse(Console.ReadLine());
if (score >= 50)
{
Console.WriteLine("Pass") // Missing semicolon - compile error
}
else
{
Console.WriteLine("Fail");
}
C# requires every statement to end with a semicolon (;). The missing semicolon is detected at compile time and the program will not run until it is corrected.
Logic Error - Program Runs With Wrong Output
What has gone wrong: the code is syntactically valid - the language rules have all been followed - but the algorithm contains a flaw in its reasoning. The program runs without any error message. The only way to detect the problem is to test it with carefully chosen values and observe that the output is incorrect.
Strategy: trace the algorithm by hand with boundary test values (e.g. the exact threshold value) before running it. A comparison error (> instead of >=) is invisible to the compiler but becomes obvious when you test with the boundary value itself.
# Python - logic error example
# Intended: pass if score >= 50
# Error: uses > instead of >= (logic error, not syntax)
score = int(input("Enter score: "))
if score > 50: # Wrong operator - 50 will give "Fail"
print("Pass")
else:
print("Fail")
# Test: enter 50 -> outputs "Fail" (should be "Pass")
# Test: enter 51 -> outputs "Pass" (correct)
# The error is only revealed by testing with boundary value 50
' VB.NET - logic error example
' Intended: pass if score >= 50
' Error: uses > instead of >= (logic error, not syntax)
Dim score As Integer = CInt(Console.ReadLine())
If score > 50 Then ' Wrong operator - 50 will give "Fail"
Console.WriteLine("Pass")
Else
Console.WriteLine("Fail")
End If
' Test: enter 50 -> outputs "Fail" (should be "Pass")
' The error is only revealed by testing with boundary value 50
// C# - logic error example
// Intended: pass if score >= 50
// Error: uses > instead of >= (logic error, not syntax)
int score = int.Parse(Console.ReadLine());
if (score > 50) // Wrong operator - 50 will give "Fail"
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Fail");
}
// Test: enter 50 -> outputs "Fail" (should be "Pass")
// The error is only revealed by testing with boundary value 50
Both Errors - Syntax Fixed, Logic Remains
This tab shows a key point: fixing a syntax error does not guarantee the program is correct. Once the syntax is fixed and the program can run, the logic error is still present - and is now the only remaining problem. This is why syntax correction and logic testing are separate activities.
# Python - syntax error fixed, logic error remains
# Step 1: original broken code (syntax error)
# if score >= 50 <- missing colon: SyntaxError
# print("Pass")
# Step 2: syntax fixed - program now runs
score = int(input("Enter score: "))
if score >= 50: # Syntax now correct
print("Pass") # But logic error remains if intended boundary differs
else
print("Fail") # Another syntax error: else: needs colon too
# A program can be syntactically correct and logically wrong simultaneously.
# Always test with boundary values after fixing syntax.
' VB.NET - syntax error fixed, logic error remains
' Step 1: original broken code (missing End If - compile error)
' If score >= 50 Then
' Console.WriteLine("Pass")
' (End If missing - compile error)
' Step 2: syntax fixed, but operator is still wrong
Dim score As Integer = CInt(Console.ReadLine())
If score > 50 Then ' > should be >= (logic error remains after syntax fix)
Console.WriteLine("Pass")
Else
Console.WriteLine("Fail")
End If
' Fixing the syntax error does not correct the logic.
' Boundary test (input = 50) is needed to reveal the remaining error.
// C# - syntax error fixed, logic error remains
// Step 1: original broken code (missing semicolons - compile errors)
// Console.WriteLine("Pass") <- missing semicolon
// Console.WriteLine("Fail") <- missing semicolon
// Step 2: syntax fixed, but operator is still wrong
int score = int.Parse(Console.ReadLine());
if (score > 50) // > should be >= (logic error remains after syntax fix)
{
Console.WriteLine("Pass");
}
else
{
Console.WriteLine("Fail");
}
// Fixing the syntax error does not correct the logic.
// Boundary test (input = 50) is needed to reveal the remaining error.
Key Takeaways
- A syntax error breaks a rule of the programming language. The program is rejected before it runs and an error message is produced, usually identifying the line.
- A logic error is a flaw in reasoning. The program runs without complaint but produces incorrect results. It is invisible to the compiler and can only be detected by testing.
- Syntax errors are easier to find because the language reports them automatically. Logic errors are more dangerous because the program appears to work.
- Fixing a syntax error does not remove a logic error. A program can be syntactically perfect and logically wrong at the same time.
- The best strategy against syntax errors is to use an IDE with real-time highlighting. The best strategy against logic errors is to trace the algorithm by hand and test with boundary values before assuming the output is correct.