GNVQ Advance IT



Unit  6 Pascal Programming 1 - an introduction


Tutor 


Pascal is a procedural programming language, Turbo Pascal is the version which we are going to use for Unit 6.2.  It provides an environment in which you can develop your programs, it has Program Development

The stages in developing a Pascal program are essentially the same as for any other language:

  1. Understand the Problem
  2. Design the solution to the problem
  3. Convert the solution into Pascal, that is, write the code in the Pascal language
  4. Remove any syntax errors from your program - this involves a cycle of compiling the program and correcting any errors until the program compiles successfully.
  5. Link the program to include any library routines
  6. Test the program until all errors - compilation, logical and run-time have been removed
A Pascal program can be divided into two parts, data declarations section and a procedural section which contains the Pascal instructions.  The data declarations must come before the instructions that refer to them.

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}


Task: 1
Code the data declarations for a Pascal program that is intended to read in the number of hours an employee has worked in a week and to calculate the gross pay.  The hourly rate is to be set 6.25 pounds per hour.

Type this code into the Pascal editor, compile and correct any syntax errors.

Task: 2

Now code the rest of the program, to print out the gross pay and the net pay.  Assume that deductions from gross pay will be 20%.



 
 Top of Page
Home Page
Query for Tutor?