GNVQ
Advance ITThe stages in developing a Pascal program are essentially the same as for any other language:
Variable Declaration
Variables are those items of data which can be changed
by the program instructions. To distinguish between these data items
they are given names. These names or identifiers can be a combination
of letters and or numbers, provided the name begins with a letter.
However, they must not be the same as a Pascal reserved word. All
variables must be declared in Pascal (though this is not necessarily true
of other languages).
Each variable declaration consists of:
variable name: data type; comment
For example:
VAR
mileage: INTEGER;{distance to work}
allowance: REAL; {travel allowance per mile}
reply: CHAR;{answer Y or N}
expenses: REAL; {monthly travelling expenses}
name: ARRAY[0..10] of CHAR;{name of max 10 char’s]
Constant declaration
In many programs there will be data values that do not
change, but remain constant throughout the program execution. An
example might be Value Added Tax - VAT, or the value of Pi. These
could be declared thus:
CONST
vat = 17.5;{current rate of VAT}
pi = 3.14159;{pi to 5 decimal places}
How do we put this in a program?
There is a set order for declaring constants and variables
in a Pascal program. The format for data declaration at the beginning
of a program is:
PROGRAM name;
{begin declarations}
CONST
VAR
{end declarations}
An example from a bicycle hiring program might look as follows.
PROGRAM bike;
{begin declarations}
CONST
summer = 10.0;{hire charge April - October}
winter = 5.0;{hire charge Nov - March
VAR season: CHAR;{A - summer, B - winter}
days: REAL;{number of days of hire}
charge: REAL;{cost of hiring bike}
{end declarations}
Type this code into the Pascal editor, compile and correct any syntax errors.
|
|
|
|