Computer ScienceChapter 2: Computational Thinking & Algorithms
2.12 Errors
An error
Explanation level
Introduction
There are two types of errors, and understanding these errors helps in developing accurate and effective problem-solving strategies. When writing programs or designing algorithms, mistakes can occur. Identifying and fixing these mistakes is an essential part of programming and computational thinking.
Definition
An error in computing is a mistake or fault in a program or algorithm that causes it to produce incorrect results, behave unexpectedly, or fail to run.
Main Idea
There are two main types of errors:
- Syntax Errors — mistakes related to the structure of an expression or statement, similar to grammatical errors in writing.
- Logical Errors — mistakes in reasoning or planning that lead to incorrect conclusions or results, akin to flawed problem-solving approaches.
Core Concept
Syntax Errors
Syntax errors are mistakes related to the structure of an expression or statement, similar to grammatical errors in writing.
- Missing semicolon in a statement.
- Missing closing bracket or parenthesis.
- Misspelled keywords.
- Incorrect indentation (in languages like Python).
Key point: Syntax errors prevent the program from running. The compiler or interpreter will catch them and show an error message.
Logical Errors
Logical errors are mistakes in reasoning or planning that lead to incorrect conclusions or results, akin to flawed problem-solving approaches.
- Using the wrong formula.
- Adding when you should subtract.
- Using the wrong variable.
- Writing a loop that never ends or ends too early.
- Incorrect comparison operators (e.g., using `>` instead of `<`).
Key point: Logical errors do NOT prevent the program from running. The program runs, but produces incorrect results.
Detailed Explanation
Example: Search Engine Query
Consider the example of a Search Engine Query, where the search engine returns irrelevant results due to both logical and syntax errors in the query.
Query: "Best smartphones for college students under 500"
Here, the user meant to exclude used phones but gets results including them. It has the logical error.
So the refined query will be:
"Best new smartphones for college students under 500."
Still the user is getting irrelevant results due to a syntax error of not writing the currency, e.g., PKR.
So the refined query will be:
"Best new smartphones for college students under PKR 500."
Comparison Table
| Feature | Syntax Error | Logical Error |
|---|---|---|
| Cause | Wrong structure/grammar | Wrong reasoning/logic |
| Detected by | Compiler/Interpreter | Testing/Dry run |
| Program runs? | No | Yes |
| Output | Error message | Incorrect result |
| Example | Missing semicolon | Using + instead of - |
| Fix | Correct the syntax | Correct the logic |
Intermediate Level
At the intermediate level, you should understand how to find and fix each type of error.
- The compiler/interpreter points them out.
- Check the line number and error message.
- Common causes: missing semicolons, brackets, quotes, or misspelled keywords.
- The program runs but gives wrong output.
- Use dry runs to trace the logic.
- Use debugging tools to step through the code.
- Add print statements to check variable values.
- Compare output with expected results.
Important Points
- - Two main types: Syntax and Logical.
- - Syntax errors: wrong structure, program does not run.
- - Logical errors: wrong reasoning, program runs but gives wrong output.
- - Syntax errors are caught by the compiler/interpreter.
- - Logical errors are caught by testing and dry runs.
- - Both types must be fixed for the program to work correctly.
Remember
- - Syntax = structure/grammar.
- - Logical = reasoning.
- - Syntax → program does not run.
- - Logical → program runs but wrong output.
- - Syntax → compiler catches.
- - Logical → testing catches.
- - Both must be fixed.
Common Mistakes
- - Confusing syntax errors with logical errors.
- - Thinking that a program with logical errors will not run (it will run).
- - Ignoring syntax error messages from the compiler.
- - Not testing enough to find logical errors.
- - Assuming that a program is correct just because it runs.
- - Forgetting to check the output against expected results.
Quick Revision
- Syntax errors: wrong structure/grammar, program does not run, caught by compiler.
- Logical errors: wrong reasoning, program runs but gives incorrect output, caught by testing/dry run.
- Example of syntax error: missing semicolon, misspelled keyword.
- Example of logical error: wrong formula, wrong operator.
- Both types must be fixed for correct program behavior.
60-Second Revision
- Syntax = structure; Logical = reasoning.
- Syntax → does not run; Logical → runs but wrong.
- Syntax → compiler catches; Logical → testing catches.
- Both must be fixed.
- Warning: A program that runs is not necessarily correct; it may have logical errors.
Did you understand this topic?
Your answer is saved with your learning record. It does not mark the topic as mastered.
Quick revision
Revision Zone
Review the key facts, formulas and checks before you move on.2.12 Errors
Sticky Notes · Quick Revision
Key Point 1
- Syntax = structure.
Key Point 2
- Logical = reasoning.
Key Point 3
- Syntax → does not run.
Key Point 4
- Logical → runs but wrong.
Key Point 5
- Syntax → compiler.
Key Point 6
- Logical → testing.
Key Point 7
- Both must be fixed.
Same supplied content as this Notes page (shared canonical source).