knowt logo

Identify Syntax & Logic Errors in Testing


What are syntax errors and logic errors?

  • Syntax errors and logic errors are thoroughly covered earlier in the course, you can find that revision note here

  • A summary of the two error types:

    • Syntax error: An error that breaks the grammatical rules of a programming language and stops it from running

    • Logic error: Incorrect code is used that allows the program to run, but produces an incorrect or undesired output

Identify Syntax & Logic Errors in Testing

How can you identify 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

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

Identifying logic errors

  • Logic errors can be slightly more 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)

    • Division by 0

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

r883LnnA_screenshot-2024-02-28-at-09-51-14

  • In this example, the incorrect Boolean 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

  • By changing the OR to AND, this 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

C

Identify Syntax & Logic Errors in Testing


What are syntax errors and logic errors?

  • Syntax errors and logic errors are thoroughly covered earlier in the course, you can find that revision note here

  • A summary of the two error types:

    • Syntax error: An error that breaks the grammatical rules of a programming language and stops it from running

    • Logic error: Incorrect code is used that allows the program to run, but produces an incorrect or undesired output

Identify Syntax & Logic Errors in Testing

How can you identify 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

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

Identifying logic errors

  • Logic errors can be slightly more 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)

    • Division by 0

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

r883LnnA_screenshot-2024-02-28-at-09-51-14

  • In this example, the incorrect Boolean 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

  • By changing the OR to AND, this 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