Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

Syntax, Logic & Runtime Errors

  • Designing algorithms is a skill that must be developed and when designing algorithms, mistakes and issues will occur

  • There are three main categories of errors that when designing algorithms a programmer must be able to identify & fix, they are:

    • Syntax errors

    • Logic errors

    • Runtime errors

What is a syntax error?

  • A syntax error is an error that breaks the grammatical rules of a programming language and stops it from running

  • Examples of syntax errors are:

    • Typos and spelling errors 

    • Missing or extra brackets or quotes

    • Misplaced or missing semicolons

    • Invalid variable or function names

    • Incorrect use of operators

    • Incorrectly nested loops & blocks of code

What is a logic error?

  • A logic error is where incorrect code is used that causes the program to run, but produces an incorrect output or result

  • Logic errors can be difficult to identify by the person who wrote the program, so one method of finding them is to use ‘Trace Tables

  • Examples of logic errors are:

    • Incorrect use of operators (< and >)

    • Logical operator confusion (AND for OR)

    • Looping one extra time

    • Indexing arrays incorrectly (arrays indexing starts from 0)

    • Using variables before they are assigned

    • Infinite loops

What is a runtime error?

  • A runtime error is where an error causes a program to crash

  • Examples of runtime errors are:

    • Dividing a number by 0

    • Index out of the range of an array

    • Unable to read or write a drive

Identifying Logic Errors

  • Logic errors can be slightly challenging to locate compared to syntax errors

  • This is because the program will still run but will not produce the expected output that the programmer intended

  • The most obvious areas to check for logic errors are:

    • Logical operators (<, >, ==, !=)

    • Boolean operators (AND, OR, NOT)

  • To help demonstrate this skill, a snippet of program code is used

r883LnnA_screenshot-2024-02-28-at-09-51-14
  • In this example, the incorrect logical operator has been used, OR instead of AND

  • The result means the else clause in the if statement would never be caught

screenshot-2024-02-28-at-09-51-20
  • At first glance, entering normal test data such as 14, the program works as intended

  • Entering erroneous data or boundary test data which is outside of the range would result in the error

    • When entering the age of 21, it still outputs that the user is in secondary school

screenshot-2024-02-28-at-09-51-35
  • Changing the OR to AND corrects the logic error

screenshot-2024-02-28-at-09-51-48
screenshot-2024-02-28-at-09-52-00

Worked Example

Dan is writing a program for maths students. To make sure that there are no logic errors in the program, Dan uses a test plan.

Describe what is meant by a logic error. [2]

Answer

  • The error does not prevent the program from running

  • But it does not produce the expected output / it does not do what the programmer intended

  • A reasonable example

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注