Being able to identify errors in a program is not only useful for your exam, but will help you to improve your own code too.
Two types of error will be shown in code – syntax errors and logic errors. Whilst both will create issues in your programs, they are treated separately.
When we talk about the syntax of a spoken language like English, we are referring to the spelling & grammar of the language. This is no different for programming languages – a programming syntax error means that there is either a spelling error in one of the key words, or there is something wrong with the way that the code has been structured.
When a syntax error occurs, the code is unable to run as the computer won’t understand it. In this case, when we try to run the code and error is shown, often with the line number and a hint of what the error might be.
The simplest example of a syntax error is in the spelling of a key word. Test your understanding of this by fixing the errors in the code below:
Logic errors are often a little bit harder to detect as most logic errors will still allow the program to run, but will create an unexpected output. There are some exceptions to this, such as trying to divide by zero which causes the program to crash as it is mathematically impossible!
Most logic errors will be an error in a calculation, or using the wrong variable name. Test your understanding of this by fixing the errors in the code below: