# INTRODUCTION TO PASCAL by cVeXWP

VIEWS: 16 PAGES: 48

• pg 1
```									INTRODUCTION TO PASCAL
PASCAL PROGRAMMING

Alexandra School 2011   1
BRIEF HISTORY
   First published in 1970.
   High level programming language named
after the famous French mathematician
Blaise Pascal.
   Designed to teach students structured
programming concepts.

Alexandra School 2011   2
WRITING PROGRAMS IN PASCAL
All Pascal program have the form:
 Declaration of constants and variables
 Main body of the program

Alexandra School 2011   3
   This states what the program does.
   It normally takes one of two forms:
 PROGRAM sumofnumbers;
 PROGRAM sumofnumbers (input, output);

 The second one is one that we will use.
 The name that you use in your program
header cannot be used anywhere else in the
program. In this example, sumofnumbers is
the name of the program.

Alexandra School 2011   4
CONSTANT DECLARATION
SECTION
   You would not need constants in every
program, but when you do, you must place
them in the Constant Declaration
Section.
   This section starts with the keyword Const
(short for ‘constant’).
   You can use as many as you want but
ONLY Constants – things that will not
change e.g pi = 3.142; days_in_week = 7;
Alexandra School 2011   5
VARIABLE DECLARATION
SECTION
   Var indicates the start of the variables
declaration section – the place where you
must declare any variables that you intend
to use in the program.
   Only variables can be declared in this
section – nothing more.
   CONST AND VAR can only appear
once.

Alexandra School 2011   6
MAIN BODY OF THE PROGRAM
This section:
 is enclosed between the words Begin and
End. (with the full stop)
 tells the computer what to do.
 When the computer encounters the full
stop, it knows that the program is
finished.
   Even though the program may contain END several
times only the one at the end will have the full stop.

Alexandra School 2011   7
Terminating Statements
   All program statements and lines are
terminated with a semi-colon, except
the Begin and End keywords. Program
statements immediately preceding an End
statement do not require a semi-colon.

Alexandra School 2011   8
VARIABLES
 Declaring a variable lets the computer
know ahead of time what type of data it is
allowed to hold.
 The computer therefore will make sure
that the user will only enter the valid
information.

Alexandra School 2011   9
DATA TYPES USED IN PASCAL
INTEGER
 Positive and negative numbers which do
NOT have a decimal point e.g -50, 79,0.

REAL
 Positive and negative numbers that may have
a decimal point, e.g 56.876, -67.8

Alexandra School 2011   10
DATA TYPES USED IN PASCAL
CHAR
• A single character (letter, digit or symbol)
•E.g a, Z, 0, #, &, )

STRING
•A group of up to 255 characters

BOOLEAN
•Either true or false (and nothing else)

Alexandra School 2011   11
DECLARING VARIABLES
   Variables must be declared using the
following format:
   Variable name: variable type;
   E.g
   Var
◦ gender: char;
◦ num2, num3: integer;
◦ results: real;
 If 2 or more variables have the same type, you can
declare them on the same line (separated with commas)
in order to save space.

Alexandra School 2011   12
VARIABLE NAMES
   The Rules below must be followed:
◦ The name must not contain a space.
◦ A variable name must only contain letters,
digits and underscores.
◦ A variable name must begin with a letter.
◦ You cannot use a reserved word as a variable
name i.e (a word that has a special meaning to
Pascal e.g Program, If, While, Begin, End.
◦ Pascal does not care whether you use
lowercase or uppercase variable names.
Alexandra School 2011   13
CONSTANTS
 A value that cannot and will not change
throughout the execution of the program.
 Const
◦ Pi = 3.142;
◦ VAT = 0.15;
◦ With constants you:
1. can (and must) specify a value.
2. don’t have to specify a type. The computer
can look at the value and figure out the
type.
3. use ‘=‘ instead of ‘:’.
Alexandra School 2011   14
 When inserting comments in your
program, you place them in curly brackets
{ }.
 Circumference:= 2 * radius * pi; {this
calculates the circumference}
 The computer completely ignores
comments so you can put anything you
want in them – even if they are not
necessarily true.
Alexandra School 2011   15
program works to other persons reading
or viewing it.
 Comments may span several lines. {This
of a circle and then uses that radius to
calculate the diameter and circumference
of the circle.}

Alexandra School 2011   16
DISPLAYING MESSAGES & THE CONTENT OF
VARIABLES ON THE SCREEN
 If you want the computer to display a
message, you must put it in single
quotes(‘).
 Writeln(‘Good morning’);
 The computer will display the message on
the screen word-for-word but without
the quotes.
 To display the value of a variable, do NOT
enclose using single quotes.
 Writeln(‘The average is: ’, avg);
Alexandra School 2011   17
DIFFERENCE BETWEEN WRITELN
AND WRITE
 Writeln command displays a message on
the screen and then moves down to the
next line.
 Write command does not move to the
next line.

Alexandra School 2011   18
FORMATTING REAL NUMBERS
 When a calculation is done and a number
like 1.0200000000E+01 displays – this is
in scientific notation.
 To fix this so that it looks like a number
you recognise, in your program you use
the following:
 Writeln (result:8:2); this means to display
to 2 decimal places and that the result
should only take up exactly 8 characters.
Alexandra School 2011   19
Writeln.
 When you run the program and the
computer gets to this line, it will wait
patiently for the user to type in some
information.
 When the user presses the Enter key, the
computer takes the information and
stores it in the variable called entirename.
Alexandra School 2011   20
IMPORTANT NOTE
 Any variables that you read MUST have
been declared in the Var section.
 When reading more than one variable
you must separate them with a comma.
 When typing in the values, you have to
separate them by using spaces.

Alexandra School 2011   21
 At sometime or another you may want
your program to pause until the user
presses the Enter key. You can do this by
using a Readln command without any
brackets. E.g

Alexandra School 2011   22
ASSIGNMENT STATEMENTS
   These allow you to change the value of a
variable. In pascal it is written like this:
◦ Variable:= expression that you want to assign
to the variable;
◦ E.gs
◦ Sex:= ‘M’; {Assigning a character constant}
◦ Cont:= count+1; {Increasing the value of a
variable}

Alexandra School 2011   23
ASSIGNMENT RULES
 You can’t assign something to a constant.
 Any variable on the right-hand side
should have already been assigned initial
values.
 The expression on the right of the :=
must be compatible with the type of
variable you have on the left.

Alexandra School 2011   24
BOOLEAN VARIABLES
 These only have two values – true/false.
 An assignment statement would look like
– found:= false;
 IsGreater:= (A>B);
◦ The computer
 Tests whether A is larger than B. Depending on the
values of A and B, it may be true or false.
 Then it stores the result of the test in the variable –
IsGreater.

Alexandra School 2011    25
IF STATEMENTS
   An IF statement in Pascal is done like this:
◦ IF condition(s) THEN
single or compound statement to be executed only
if the condition is true;
E.G If a person is more than 65, then call that person
old.
IF age > 65 THEN
Writeln(‘You are really old’);

Alexandra School 2011   26
 E.g A discount is given of 15% if the
customer spends \$150.
IF total >= 150 Then
Begin
discount:= 15/100 *total;
total:= total – discount;
of ‘, discount);
End;

Alexandra School 2011   27
IF-THEN-ELSE
IF (Boolean expression) THEN
BEGIN
Action 1
END
ELSE
BEGIN
Action 2
END;

Alexandra School 2011   28
E.g
IF (mark>40)THEN
WRITELN (‘Pass’)
ELSE
WRITELN (‘Fail’);
OR
IF (mark>40) THEN
BEGIN
WRITELN (‘Pass’);
END
ELSE
BEGIN
WRITELN (‘Fail’);
END;

Alexandra School 2011   29
FOR
FOR <integer variable> := <integer> TO
<integer> DO
BEGIN

END;

Alexandra School 2011   30
WHILE
WHILE <condition> DO
single or compound statement to be
repeated;
E.G
While num <>999 DO
BEGIN
Square:= num*num;
Writeln(Square);
END;
Alexandra School 2011   31
REAPEAT …UNTIL …
   Is used to loop 1 or more times. It is similar
to the While loop except that it tests the
condition at the bottom of the loop.
   In Pascal this is:
REPEAT
…..
UNTIL (Boolean expression)
REPEAT

Alexandra School 2011   32
ARRAYS
 A collection of data items of the same
type and referred to by a single name.
The location or position of each item is
uniquely designated by an integer
subscript.
 It allows a number of variables of the
same type to be grouped and manipulated
together.

Alexandra School 2011   33
   A visual representation of an array with 5 items.

Subscript          Numbers
1                  102
2                  95
3                  113
4                  109
5                  106

 The Numbers column shows the items entered.
 Subscript refers to the location or position of the
items as previously mentioned.

Alexandra School 2011   34
Accessing Data in an Array
 Each data item in the array is accessed by
using the array name and a subscript – for
example
 STUDENTS[2] is the second position in
the array called STUDENTS and contains
95. STUDENTS[3] is the third position
and contains 113, etc.

Alexandra School 2011   35
Declaring Arrays
   Arrays are declared in almost the same
way as normal variables except that you
have to say how many items you want in
the array.
 VAR name of array: ARRAY [1..Maximum index]
of data type;
 E.g.
VAR Students: ARRAY [1..5] of integer;

Alexandra School 2011   36
Initialising an Array
 Initialising and array gives ALL locations in
the array a starting value, usually zero for
an integer array or a space for character
arrays.
 We use For loops to assign values.
 E.g.
for i := 1 to 5 do
Students [i] := 0;

Alexandra School 2011   37
Assigning Values to the Array
 E.g
for i := 1 to 5 do
begin
write (‘Enter the number of students
in each year group’, i);
 End;

Alexandra School 2011   38
Displaying the Contents of an Array
 E.g
for i := 1 to 5 do
begin
write (‘Number of students in year’, i);
writeln (students[i]);
 End;

Alexandra School 2011   39
Array Output
 Number of students in year 1: 102
 Number of students in year 2: 95
 Number of students in year 3: 113
 Number of students in year 4: 109
 Number of students in year 5: 106

Alexandra School 2011   40
DOCUMENTING YOUR
PROGRAM
 To help other users learn how to use
 It helps programmers to:
◦ understand (or remember) what each section
of code does
◦ see any assumptions that were made when
writing
◦ be made aware of any peculiarities the
program may have.

Alexandra School 2011   41
Internal Documentation
   Documentation that is inside the source
code of the program.
 To facilitate internal documentation.
 It explains sections of the program – explaining
each step along the way.
 Explain what a particular variable stores.

 NB – you don’t have to comment on everything.
 Use self explanatory variable names.

Alexandra School 2011   42
Indentation
   This is done to make it easy for
   It helps to identify quickly which code is
inside a particular construct and which isn’t.
   It is convention to indent:
◦   Code between a Begin … End block.
◦   Declarations in the Var and Const sections.
◦   The code in the IF and Else parts of IF statement.
◦   The code inside loops.

Alexandra School 2011     43
White Space
   These are things like blank lines and
spaces. These can be used to visually
divide your code into sections, making it
easier for programmers to identify which
pieces of code are related to a particular

Alexandra School 2011   44
External Documentation
 Documentation that is outside the
program’s source code.
 Two main types are:
◦ User manuals
◦ Technical manuals

Alexandra School 2011   45
User Manuals
   These tell the user how to use the
program. They include:
◦ The hardware and software requirements of
the program
◦ Instructions on how to install and start the
program
◦ Explanations on what certain menu and
toolbar items do
◦ Instructions on how to use certain features
◦ Troubleshooting guides.

Alexandra School 2011   46
Technical Manuals
   These documents are meant for
programmers and they explain how the
program was written so that future
modifications can be made. They may
include:
◦   the algorithm
◦    a program listing
◦   Test cases and expected results
◦   Information about data file structures
◦   Known problems and workarounds.

Alexandra School 2011   47
INFORMATION TAKEN FROM
 Information Technology for CSEC –
Kelvin Skeete/Kyle Skeete
 Information Technology for CSEC
Examinations – Howard Campbell and
Alan Wood

Alexandra School 2011   48

```
To top