Program Errors: Syntax, Logic and Runtime

Master debugging skills and understand the three main types of program errors – essential for CSEC IT success!

🎯 Learning Objectives

By the end of this article, you should be able to:

  • Define syntax, logic, and runtime errors
  • Identify different types of errors in algorithms and programs
  • Explain the causes and effects of each type of error
  • Debug simple programs and algorithms
  • Answer CSEC exam questions involving program errors
1

Introduction to Program Errors

Program errors happen to everyone! Just like making mistakes when learning a new language or solving math problems, errors are a normal part of programming. Even experienced programmers encounter errors daily.

💭 Think About It

Remember learning algebra? If you wrote “3 + = 5” instead of “3 + x = 5”, that’s like a syntax error. If you solved “2 + 2” and wrote “5” instead of “4”, that’s like a logic error. Program errors work the same way!

Why Do Errors Occur?

  • Human mistakes: Typos, forgetting punctuation, incorrect formulas
  • Misunderstanding requirements: Solving the wrong problem
  • Edge cases: Not considering all possible inputs (like dividing by zero)
  • Complex logic: Difficult algorithms with many steps
2

What Is a Program Error?

A program error (bug) is any mistake in a program that causes it to behave unexpectedly or not work at all. Errors can be detected at different stages:

🚫

Before Execution

Syntax errors are caught by the compiler or interpreter before the program runs. The program won’t start until these are fixed.

🤔

During Execution

Logic errors allow the program to run but produce wrong results. These are often the hardest to find!

💥

At Specific Moments

Runtime errors occur when the program tries to do something impossible while running.

3

Syntax Errors: Breaking the Rules

Syntax errors occur when you break the grammar rules of a programming language. Just as “Me go store” is incorrect English syntax, programming has its own rules that must be followed exactly.

Common Syntax Error Examples

PRINT “Hello World” // Missing closing quotation mark
x = 5 + * 3 // Invalid operator combination
IF age > 18 THEN // Missing END IF in some languages
FOR i = 1 TO 10 // Missing NEXT statement
🔍 Spot the Error Challenge

Can you find all 3 syntax errors in this pseudocode?

BEGIN
INPUT score
IF sore > 50 THEN // Error 1: Misspelled variable
PRINT “Pass”
ELSE
PRINT “Fail”
// Error 2: Missing END IF
total = total score // Error 3: Space in variable name
END
4

Logic Errors: The Program Works… But Wrong!

Logic errors are the most subtle type of error. The program runs without crashing, but it produces incorrect results because the programmer’s logic is flawed.

Real-World Example

Imagine calculating a 20% discount:

discount = price * 0.2
finalPrice = price + discount // ERROR: Should be SUBTRACT!
// Result: $100 item → $120 (WRONG!)
discount = price * 0.2
finalPrice = price – discount // CORRECT: Subtract discount
// Result: $100 item → $80 (CORRECT!)

Common Logic Error Scenarios

  • Incorrect conditions: Using > instead of >=
  • Wrong formulas: Area = length * width (instead of length * height)
  • Infinite loops: Forgetting to increment a counter
  • Off-by-one errors: Looping one time too many or too few
5

Runtime Errors: When Execution Fails

Runtime errors occur while a program is running, usually when it tries to perform an impossible operation. The program compiles and starts successfully but crashes during execution.

Classic Runtime Error Examples

Division by Zero

x = 10
y = 0
result = x / y // CRASH! Can’t divide by zero
📊

Array Out of Bounds

scores[5] = {90, 85, 95, 88, 92}
PRINT scores[10] // CRASH! Index doesn’t exist
🔤

Invalid Input Type

INPUT age
IF age > 18 THEN…
// User enters “eighteen” instead of 18
6

Comparing Syntax, Logic and Runtime Errors

Error Type When Detected Program Runs? Example How to Fix
Syntax Error Before execution (compile time) ❌ No PRINT “Hello // Missing closing “ Check language rules, fix typos
Logic Error During testing (output is wrong) ✅ Yes, but incorrectly Area = length * width // Should be length * height Trace algorithm, test with different data
Runtime Error During execution (at specific moment) ⚠️ Starts then crashes result = 10 / 0 // Division by zero Add validation, handle exceptions
🎮 Error Classification Game

Classify these errors. Drag your answers mentally, then check:

1. PRINT "The answer is " + answer (missing semicolon in some languages)

2. average = (score1 + score2) / 2 (should divide by 2.0 for decimal result)

3. OPEN "data.txt" FOR READING (file doesn’t exist)

7

Debugging Techniques: Finding the Bug

Systematic Debugging Strategies

  • Tracing: Follow the program step-by-step with sample data
  • Desk Checking: Manually execute the algorithm on paper
  • Breakpoints: Pause execution at specific points to check values
  • Print Statements: Display variable values at key moments

CSEC-Specific Debugging Tips

📝 Exam Strategy

In CSEC exams, when debugging algorithms:

  1. Read carefully: Look for missing END IF, NEXT, or END WHILE
  2. Test edge cases: What if input is 0? Negative? Very large?
  3. Check loop boundaries: Does it loop one too many/few times?
  4. Verify calculations: Manually check formulas with simple numbers

Debug This Algorithm

BEGIN
total = 0
count = 0
INPUT number
WHILE number != -1 DO
total = total + number
count = count + 1
INPUT number
END WHILE
average = total / count // Potential runtime error!
PRINT “Average: “, average
END

Can you spot the potential runtime error?

9

Summary & Next Steps

Key Takeaways

  • Syntax errors = Grammar mistakes → caught before running
  • Logic errors = Wrong thinking → program runs but gives wrong answers
  • Runtime errors = Impossible operations → program crashes during execution
  • Debugging requires patience, systematic testing, and attention to detail

Common CSEC Exam Mistakes to Avoid

⚠️ Watch Out!
  • Confusing syntax with logic errors
  • Not testing algorithms with multiple input values
  • Forgetting to check for division by zero
  • Missing END statements in pseudocode
  • Not reading algorithm tracing questions carefully

Practice Makes Perfect!

Try these activities to improve your debugging skills:

  1. Find and fix errors in sample CSEC past paper algorithms
  2. Write test cases for simple programs (normal, edge, invalid inputs)
  3. Trace algorithms with different starting values
  4. Explain errors in your own words to a study partner
Scroll to Top