Back to 课程

Computer Science GCES AQA

0% Complete
0/0 Steps
  1. Representing Algorithms Aqa
    4 主题
  2. Efficiency Of Algorithms Aqa
    1 主题
  3. Searching Algorithms Aqa
    3 主题
  4. Sorting Algorithms Aqa
    3 主题
  5. Data Types Aqa
    1 主题
  6. Programming Concepts Aqa
    5 主题
  7. Arithmetic Relational And Boolean Operations Aqa
    1 主题
  8. Data Structures Aqa
    3 主题
  9. String Manipulation Aqa
    1 主题
  10. Random Number Generation Aqa
    1 主题
  11. Structured Programming Aqa
    2 主题
  12. Robust And Secure Programming Aqa
    4 主题
  13. Number Bases Aqa
    2 主题
  14. Converting Between Number Bases Aqa
    3 主题
  15. Units Of Information Aqa
    9 主题
  16. Hardware And Software Aqa
    4 主题
  17. Boolean Logic Aqa
    3 主题
  18. Programming Languages And Translators Aqa
    2 主题
  19. Cpu Architecture Performance And Embedded Systems Aqa
    4 主题
  20. Memory Aqa
    2 主题
  21. Secondary Storage Aqa
    3 主题
  22. Fundamentals Of Computer Networks Aqa
    8 主题
  23. Fundamentals Of Cyber Security Aqa
    1 主题
  24. Methods Of Preventing Cyber Security Threats Aqa
    1 主题
  25. Relational Databases Aqa
    2 主题
  26. Ethical Legal And Environmental Impacts Aqa
    2 主题
课 Progress
0% Complete

Exam code:8525

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

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

    • Syntax errors

    • Logic errors

Syntax & Logic 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

Examples

Syntax Errors

Corrected

age = input("Enter age)

favNum == input("Enter favourite number")

print age + favNum)

print (age x favNum)

age = input("Enter age") # Missing "

favNum = input("Enter favourite number")

# Only one equal sign

print (age + favNum) # Missing bracket

print (age * favNum) # Multiply symbol is *

num1 = imput("Enter the first number")

num2 = input(Enter the second number)

if num1 > num2 then

print(num1 + " is larger")

elseif num2 > num1 then

Print(num2 + " is larger")

else

print("The numbers are the same")

endif

num1 = input("Enter the first number")

# Misspelt word 

num2 = input("Enter the second number")

# Missing quotes

if num1 > num2 then

print(num1 + " is larger") # Block

not indented

elseif num2 > num1 then

print(num2 + " is larger") # Lowercase p

else

print("The numbers are the same")

endif

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

Example

  • An algorithm is written to take as input the number of miles travelled. The algorithm works out how much this will cost, with each mile costing £0.30 in petrol. If this is greater than £10.00 then it is reduced by 10%.

Logic errors

Corrected

miles = input("Enter the number of miles)

 cost = 0.3

 if cost = 10 then

cost = cost * 0.1

 endif

 print(cost)

miles = input("Enter the number of miles")

cost = miles * 0.3

if cost > 10 then

cost = cost * 0.9

endif

print(cost)

Commentary

  • The cost was set to 0.3 (30p) instead of correctly calculating the cost of the trip by applying the formula, miles * 0.3

  • The cost should only be reduced if the cost is greater than 10, in the original algorithm it only checked if the cost was equal to 10

  • To calculate a discount of 10%, either calculate what 10% is and subtract it from the original or multiply the full cost by 0.9. In the original algorithm it calculates what 10% is and sets the cost to equal it.

Identify & Categorise Errors

How can you identify and categorise errors?

  • As a result of a syntax error breaking the rules of the programming language, the program will not execute

  • These are easily identifiable as the IDE will provide information about what the error is

    • This is done to help the programmer be able to fix the issue

  • To help practise this skill, a selection of program screenshots will be used

The errors

  • In the code below, there is a program which allows the user to enter how many items they wish to add to a shopping list

  • The code then allows the user to

    • Enter their chosen number of items into a list

    • Output the list to the screen

  • The code contains 3 syntax errors, highlighted with blue boxes

MN6hXX4I_screenshot-2024-02-28-at-09-16-13

Error 1

  • Line 3: Missing a second bracket at the end of integer input

    • The error message below is what the IDE provided to help the programmer identify the error

    • A top tip when programming is to look at the line of code above the one given in the error message

      • For example, this error message claims line 6 is the issue, however, the code above line 6 (line 3) is the line that contains the error

screenshot-2024-02-28-at-09-17-20

Error 2

  • Line 6: Missing a colon at the end of a for loop, while loop or if statement

    • As mentioned earlier, the error message identifies the line after the actual error

    • The error is on line 6, however, the syntax error message identifies the line below it

      • Always check the line above for any potential errors

screenshot-2024-02-28-at-09-18-48

Error 3

  • Line 7: Using == which is used for a comparison instead of a single = to declare a variable

    • This would return the same error as above, this time the user would more easily be able to identify the issue as it is on line 7

screenshot-2024-02-28-at-09-18-48

The fix

  • Once all fixes are in place, the code should appear as follows and the program should execute as intended

screenshot-2024-02-28-at-09-15-41
<img alt=”screenshot-2024-02-28-at-09-17-50″ class=”ContentBlock_figure__vJw2q” data-nimg=”1″ decoding=”async” height=”154″ loading=”lazy” sizes=”(max-width: 320px) 320w, (max-width: 640px) 640w, (max-width: 960px) 960w, (max-width: 1280px) 1280w, 1920w” src=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=3840/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png” srcset=”https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=16/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 16w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=32/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 32w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=48/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 48w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=64/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 64w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=96/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 96w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=128/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 128w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=256/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 256w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=384/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 384w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=640/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 640w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=750/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 750w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=828/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 828w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=1080/https://cdn.savemyexams.com/uploads/2024/02/screenshot-2024-02-28-at-09-17-50.png 1080w, https://cdn.savemyexams.com/cdn-cgi/image/f=auto,width=1200/https://cdn.savemyexams.com/upload

Responses

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