Producing robust programs - OCRIdentify syntax and logic errors

Programs must run correctly or they are of little value. Careful planning and testing of a program are essential, as is writing maintainable code.

Part ofComputer ScienceComputational thinking, algorithms and programming

Identify syntax and logic errors

When developing there are two types of error () that often occur:

Syntax errors

A syntax error occurs when code written does not follow the rules of the programming language. Examples include:

  • misspelling a statement, eg writing pint instead of print
  • using a before it has been declared
  • missing brackets, eg opening a bracket but not closing it

A program will not run if it has syntax errors. Any such errors must be fixed first. A good integrated development environment (IDE) will usually point out any syntax errors to the programmer.

Logic errors

A logic error is an error in the way a program works. The program simply does not do what it is expected to do.

Logic errors can have many causes, such as:

  • incorrectly using logical operators, eg expecting a program to stop when the value of a variable reaches 5, but using <5 instead of <=5
  • incorrectly using
  • unintentionally creating a situation where an may occur
  • incorrectly using brackets in calculations
  • unintentionally using the same variable name at different points in the program for different purposes
  • referring to an element in an that falls outside of the scope of the array

Unlike a syntax error, a logic error will not usually stop a program from running. Instead the program will run but not function as expected.

What happens when there is a logic or syntax error in a program?