Programming errors
When developing programSequences of instructions for a computer. there are three types of error that can occur:
- syntax errorError in a program resulting from code not following syntax rules governing how to write statements in a programming language.
- logic errorError in a program which does not cause a program to crash but causes unexpected results.
- runtime errorAn error that takes place during the running of a program.
Syntax errors
A syntax error occurs when the code given does not follow the syntax rules of the programming language. Examples include:
- misspelling a statement, eg writing
pint
instead ofprint
- using a variableA memory location within a computer program where values are stored. before it has been declared
- missing brackets, eg opening a bracket, but not closing it
A program cannot run if it has syntax errors. Any such errors must be fixed first. A good integrated development environment (IDE)A suite of tools that helps a programmer to write error-free, maintainable code. usually points out any syntax errors to the programmer.
Logic errors
A logic error is an error in the way a program works. The program can run but does not do what it is expected to do.
Logic errors can be caused by the programmer:
- 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 Boolean operatorAND, OR and NOT. Used to build complex queries in a database.
- unintentionally creating a situation where an infinite loopA loop that repeats forever, ie indefinitely. may occur
- incorrectly using brackets in calculations
- unintentionally using the same variable name at different points in the program for different purposes
- using incorrect program design
Unlike a syntax error, a logic error does not usually stop a program from running. The program will run, but not function as expected.
Runtime errors
A runtime errorAn error that takes place during the running of a program. is an error that takes place during the running of a program.
An example is writing a program that tries to access the sixth item in an arrayA set of data values of the same type, stored in a sequence in a computer program. Also known as a list. that only contains five items. A runtime error is likely to crash the program.