Titlebar
Home FAQ and Resources 2.1.1 Fundamentals

2.1.2 Hardware

2.1.3 Software 2.1.4 Data 2.1.5 Databases 2.1.6 Comms & networks 2.1.7 Programming


 

2.1.7 Programming


a) Understanding algorithms

b) Pseudocode & flow diagrams

c) High level v machine code

d) Introduction to translators

e) Characteristics of translators

f) IDEs

g) Sequence

h) Selection

i) Iteration

j) Variables and constants

k) Use variables and constants

l) Introduction to data types

m) Justifying data types

n) Common operations on data

o) One dimensional arrays

p) Syntax and logic errors

q) Understanding errors

r) Test data

 


 





2.1.7 p) and q) Syntax and logic errors

Introduction
Programmers rarely write perfect programs the first time round. They can make mistakes, which either stop the program from being translated from source code into object code, or which does translate into object code but gives the incorrect answer when the code is executed. Programmers need to know what sort of errors they might expect and also how to track them down.

Testing

Syntax errors
Sometimes, when you tell your Integrated Development Environment (IDE) to translate a program from source code to object code, you get some error messages. The translator (whether it is a compiler or an interpreter) cannot convert the code. This is because the rules of the programming language you are using have been broken. It might be that you have not spelled a 'reserved word' correctly. It might be that you haven't used a ' reserved word' in the correct way. (A 'reserved word' is a word that is used by the programming language - you are not allowed to use it as an identifier.)

Syntax rules
Consider the English sentence, 'The dog bites the man.' This follows the rules for the English language. The sentence has a capital letter at the start and a full stop at the end. All the words have been spelled correctly. All the words are in the correct order (we haven't written for example, 'dog the man the bites.' The first sentence follows the 'syntax rules' for the English language. The second sentence doesn't.

Consider part of a program we have written:

WRITE "Press C to continue"
READ KeyPress
WHILE (KeyPress NOT EQUAL TO C) DO
BEGIN.
    WRITE "Press C to continue"
    READ KeyPress
END
ENDWHILE

When we wrote this program, it looked like it was going to work but when we tried to compile it, it wasn't translated into object code. We got an error message back from the error diagnostics tools, saying that there was a problem around the BEGIN statement. If we look carefully, we can see a full stop. This shouldn't be there. It breaks the rules for using the reserved word 'BEGIN'. You are not allowed to put a full stop after BEGIN. This is an example of a 'syntax error'.

Compilers and translators check the syntax of a program. If they find a problem, the 'error diagnostics tools' try to help. If an error is found, the error diagnostics report it by displaying an error message and attempts to indicate where the error is in the program. Some problems that programmers often face when using error diagnostics, however, are:

  • error messages from the error diagnostics tools are often difficult to understand
  • one error can cause lots of other errors to be displayed, even though there are no other errors
  • a reported error is sometimes not at the place where the 'error diagnostics' tools say it is - you have to look carefully just before and just after that position.

Logical errors
Sometimes, a programmer will write a statement where the syntax is perfectly correct and the translators translate the source code into object code correctly. However the wrong answer is given when the program is run! This type of error is known as a 'logical error'. They can be quite hard to spot unless a program is tested thoroughly.

For example, if a programmer wrote this:

MonthsInYear = 13
Average = Total / MonthsInYear

and compiled it, it would compile and run okay. It would give you an answer. However, it would give an incorrect answer (there are only 12 months in a year).

Another example - if a programmer wrote this:

a = b + c

and compiled it, it would compile and run okay. However, it would give an incorrect answer because the programmer meant to write:

a = b - c

These are examples of 'logical errors'.

Q1. What is meant by a ‘reserved word’?
Q2. What type of error might be displayed when you try to compile a program?
Q3. What is a ‘logical error’?
Q4. Give an example of a logical error.

 
 

 

V1.0 Copyright theteacher.info Ltd 2011