Back to 课程

Computer-science_A-level_Cie

0% Complete
0/0 Steps
  1. computers-and-components
    6 主题
  2. logic-gates-and-logic-circuits
    2 主题
  3. central-processing-unit-cpu-architecture
    6 主题
  4. assembly-language-
    4 主题
  5. bit-manipulation
    1 主题
  6. operating-systems
    3 主题
  7. language-translators
    2 主题
  8. data-security
    3 主题
  9. data-integrity
    1 主题
  10. ethics-and-ownership
    3 主题
  11. database-concepts
    3 主题
  12. database-management-systems-dbms-
    1 主题
  13. data-definition-language-ddl-and-data-manipulation-language-dml
    1 主题
  14. computational-thinking-skills
    1 主题
  15. algorithms
    14 主题
  16. data-types-and-records
    2 主题
  17. arrays
    2 主题
  18. files
    1 主题
  19. introduction-to-abstract-data-types-adt
    1 主题
  20. programming-basics
    1 主题
  21. constructs
    2 主题
  22. structured-programming
    1 主题
  23. program-development-life-cycle
    2 主题
  24. program-design-
    2 主题
  25. program-testing-and-maintenance
    3 主题
  26. user-defined-data-types
    1 主题
  27. file-organisation-and-access-
    3 主题
  28. floating-point-numbers-representation-and-manipulation
    3 主题
  29. protocols
    2 主题
  30. circuit-switching-packet-switching
    1 主题
  31. processors-parallel-processing-and-virtual-machines
    5 主题
  32. boolean-algebra-and-logic-circuits
    4 主题
  33. purposes-of-an-operating-system-os
    3 主题
  34. translation-software
    3 主题
  35. encryption-encryption-protocols-and-digital-certificates
    3 主题
  36. artificial-intelligence-ai
    4 主题
  37. recursion
    1 主题
  38. programming-paradigms
    4 主题
  39. object-oriented-programming
    7 主题
  40. file-processing-and-exception-handling
    2 主题
  41. data-representation
    5 主题
  42. multimedia
    3 主题
  43. compression
    2 主题
  44. networks-and-the-internet
    11 主题
课 Progress
0% Complete

Error types

What are the common types of error?

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

  • Trace tables can also help to find any kind of error in a program or algorithm

  • 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

Syntax 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

Pseudocode – with syntax errors

FUNCTION generate_username(first_name STRING, last_name STRING) RETURNS STRING DECLARE username : STRING username = SUBSTRING(first_name, 1, 1) + SUBSTRING(last_name, 1, 1) + SUBSTRING(last_name, 1, 3) RETURN username
ENDFUNCTION PROCEDURE main() DECLARE first_name : STRING DECLARE last_name : STRING DECLARE username : STRING OUTPUT "Enter your first name: " INPUT first_name OUTPUT "Enter your last name: " INPUT last_name username ← generate_username[first_name, last_name] OUTPUT "Here's a suggested username:" + username
ENDPROCEDURE // Main program
CALL Main

Pseudocode – without syntax errors

FUNCTION generate_username(first_name : STRING, last_name : STRING) RETURNS STRING DECLARE username : STRING username ← SUBSTRING(first_name, 1, 1) + SUBSTRING(last_name, 1, 1) + SUBSTRING(last_name, 1, 3) RETURN username
ENDFUNCTION PROCEDURE main() DECLARE first_name : STRING DECLARE last_name : STRING DECLARE username : STRING OUTPUT "Enter your first name: " INPUT first_name OUTPUT "Enter your last name: " INPUT last_name username ← generate_username(first_name, last_name) OUTPUT "Here's a suggested username: ", username
ENDPROCEDURE // Main program
CALL main()

Syntax errors

  • FUNCTION generate_username(first_name STRING...)

    • Missing colon : between parameter name and type

    • first_name : STRING and last_name : STRING

  • username = SUBSTRING(...)

    • Uses = instead of correct assignment operator ←

    • Should be username ← ...

  • username ← generate_username[first_name, last_name]

    • Incorrect use of square brackets [] instead of parentheses () for function call

    • Should be generate_username(first_name, last_name)

  • CALL Main

    • Incorrect capitalisation and missing parentheses — procedure name is case-sensitive

    • Should be CALL main()

Logic errors

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

Pseudocode

FUNCTION calculate_area(length : REAL, width : REAL) RETURNS REAL // Checks for valid dimensions and calculates area IF length > 0 OR width > 0 THEN DECLARE area : REAL area ← width - length RETURN area ELSE OUTPUT "Length and width must be positive values." ENDIF
ENDFUNCTION PROCEDURE main() DECLARE length : REAL DECLARE width : REAL DECLARE area : REAL OUTPUT "Enter the length of the rectangle: " INPUT length OUTPUT "Enter the width of the rectangle: " INPUT width area ← calculate_area(length, width) OUTPUT "The area of the rectangle is approximately ", area, " square units."
ENDPROCEDURE // Main program
CALL main()

Logic errors

Line

Error

Correction

IF length > 0 OR width > 0 THEN

The check allows one value to be negative, which is not valid

Should be IF length > 0 AND width > 0 THEN

area ← width - length

Incorrect calculation – subtracts instead of multiplying to find the area

Should be area ← length * width

Runtime errors

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

Pseudocode

FUNCTION calculate_area(length : REAL, width : REAL) RETURNS REAL // Calculates area of a rectangle after input validation IF length < 0 OR width < 0 THEN OUTPUT "Length and width must be positive values." ENDIF DECLARE area : REAL area ← length * width RETURN area
ENDFUNCTION PROCEDURE main() DECLARE length : REAL DECLARE width : REAL DECLARE area : REAL OUTPUT "Enter the length of the rectangle: " INPUT length OUTPUT "Enter the width of the rectangle: " INPUT width area ← calculate_area(length, width) OUTPUT "The area of the rectangle is approximately ", area, " square units."
ENDPROCEDURE // Main program
CALL main()

Runtime error

Line

Error

Correction

area ← calculate_area(length)

Only one parameter is passed instead of two – will crash at runtime

Should be area ← calculate_area(length, width)

Worked Example

Each pseudocode statement in the following table may contain an error due to the incorrect use of the function or operator.

Describe the error in each case, or write ‘NO ERROR’ if the statement contains no error.

You can assume that none of the variables referenced are of an incorrect type.[5]

Statement

Error

Result ← 2 & 4

SubString ← MID("pseudocode", 4, 1)

IF x = 3 OR 4 THEN

Result ← Status AND INT(x/2)

Message ← "Done" + LENGTH(MyString)

Answer

Statement

Error

Result ← 2 & 4

Should be arithmetic

operator (not &) OR 2 and 4

should be CHAR / STRING

SubString ← MID("pseudocode", 4, 1)

NO ERROR

IF x = 3 OR 4 THEN

Not Boolean values /

incorrect operator OR

Condition incorrect

Result ← Status AND INT(x/2)

INT(X/2) doesn’t evaluate

to a Boolean value /

incorrect operator

Message ← "Done" + LENGTH(MyString)

Can’t add string to number /

“Done” is not a number

Responses

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