Pascal Programming
Pascal Programming
PROGRAMMING
INTRODUCTION
PASCAL is a programming language named after the 17th century mathematician
Blaise Pascal.
Identifiers: In the previous example, TITLE is the name the programmer gave to
the Pascal program. It is an identifier. Identifiers begin with a letter, then
followed by any digit, letter or the underscore character ( _ ).
INPUT
In Pascal we use two commands together in order to receive input from the
user. We first use a writeln or write statement to display a message explaining
the type of input required and then a readln statement to store the data in an
appropriate variable, for example:
writeln('What is your name?');
readln(name);
N.B. the single quote (‘) characters around the question. There is no need to use
single quotes in the readln line. Each line ends with a semi-colon ;
writeln – displays a message and sends the cursor in the output screen to the next
line without pressing ‘Enter’.
write – displays a message but does not sent the cursor to the next line
readln – reads the data to be stored on separate lines
read – reads the data to be stored on the same line. Each piece of data is usually
separated by a space.
INPUT, PROCESS & OUTPUT CONT’D
PROCESS
Processing usually involves changing values of variables or creating extra variables
by combining other variables e.g. adding two numbers together to come up with
third. In Pascal we use the assign := sign to perform calculation. While it looks
very similar to the equals = symbol it is different. Equals (=) is used for
comparisons while assign (:=) is for storing values. All types of variables can have
values placed in them using the assign sign, for example:
N.B. Single quotation marks are only required for storing strings or characters
INPUT, PROCESS & OUTPUT CONT’D
OUTPUT
The final stage is the outputting of processed data or information to the user. Once
again we use the writeln or write statements. The basic rule to follow is that text
that you create in contained within single quotation marks (') and variables are
contained between commas (,) unless they are at the start or end of the output, for
example:
writeln('Thank you for using this program.');
writeln(name,' is in Year 10');
writeln(name,' will turn ',age,' in 2000');
It the very precise way in which the statements in a program must be written in
order to be understood. The syntax allows the programmer to create a correctly
structured statement that is ‘legal’ although it does not guarantee that the
statement will be useful:
⚫ All program statements and lines are terminated with a semi-colon (;)
⚫ A semi-colon is not required after any of the Pascal keywords
⚫ The program name and variables must be one word, no spaces
⚫ The program name and variable names cannot match
⚫ Variables of each type must be declared separately
⚫ The use of indentation and spacing helps in the presentation and readability of
the coding
⚫ After the last END, a full stop (.) is required
PROGRAM STRUCTURE
Nearly all structured programs share a similar overall structure:
⚫ Statements to establish the start of the program
⚫ Variable declarations
⚫ Program statement(s) – blocks of code
Example 1: Example 2:
Eg.) Write a program which prompts the user to enter the radius of a circle and calculate the area of
the circle. [Area of a circle = πr2]
2. Write a program to request a number and print the number and its
square.
4. Write a program to which prompts the user to enter two integer values
L and B, which represents the length and breadth of a rectangle, and
calculates the area of the rectangle.
Syntax:
IF (Boolean expression) THEN
statement1
ELSE
statement2 ; N.B. the semi-colon is placed after the statement
following the ELSE.
Eg.) The following piece of code checks the age of the person to see if he/she is eligible to vote.
The person must be 18 years and over to vote.
This algorithm prompts the user to input the age of a {This program prompts the user to input the age of a
person and prints whether he/she is a student, a person and prints whether he/she is a student, a
Start Begin
Print(‘Please enter the age of the person’) writeln(‘Please enter the age of the person’;
Input(age) readln(age);
IF (age <= 18) AND (age >=5) THEN IF (age <= 18) AND (age >=5) THEN
ELSE ELSE
Stop End.
EXERCISE #1
1. Code the algorithms from Exercise 2 in Pascal.
2. Write a program to request a score in a test and print a letter grade based on the
following:
score < 50 F
50 ≤ score ≤ 65 C
65 ≤ score < 80 B
score ≥ 80 A
3. Given 3 integer values representing the sides of a triangle, write a program to print:
⚫ Not a triangle – if the any of the values are negative or zero
⚫ Sorry !– if the length of any side is greater than or equal to the sum of the other
2 sides
⚫ Isosceles – if the triangle has two equal sides
⚫ Equilateral – if all three sides are the same
⚫ Scalene – if all three sides are different
THE ‘FOR’ LOOP
Eg.) A man works 40 hours per week and he is paid an hourly rate of $50.00 per
hour. Write a program to read the names and hours worked for 10 employees.
Determine and print their weekly wages along with their names.
Declaring an array
An array is usually declared in the ‘var’ section. The syntax is as follows:
Pseudocode: ArrayName[lower..upper] of data type
Pascal Code: ArrayName = Array[lower..upper] of data type
23 12 56 19 34
numbers[1] numbers[2] numbers[3] numbers[4] numbers[5]
INITIALISING ARRAYS
Pseudocode Pascal Code
Initialising an array of integers or real: Initialising an array of integers or real :
FOR x = 1 to n DO FOR x:= 1 to n DO
ArrayName[x] = 0 ArrayName[x] := 0;
Start Begin
Set sum to 0 sum := 0;
FOR n = 1 to 10 DO FOR n := 1 to 10 DO
Print(‘Please enter a mark’) Begin
Read(Marks[n]) writeln(‘Please enter a mark’);
Set sum to sum + Marks[n] readln(Marks[n]);
EndFOR sum:= sum + Marks[n];
Set average to sum/10 end;
Print(‘The sum is: ‘, sum) average:= sum/10;
Print(‘The average is: ‘, average) writeln(‘The sum is: ‘, sum);
Stop writeln(‘The average is: ‘, average);
End.
SEARCHING AN ARRAY
The following is a standard algorithm for searching for a particular item in an array:
Set Found to 0
‘item’ can be an integer, character or a string and refers
FOR x = 1 to n DO to an element in an array which is to be located.
IF item = ArrayName[x] THEN
set Found to x
set x to n setting x to n ‘forces’ exit out of the FOR loop once the item is found
EndIF
EndFOR
IF (Found = 0) THEN
Print(item,’ is not found’)
ELSE
Print(item,’ is found in ‘, Found)
EndIF
Eg 2.) Read in the marks of 10 students and store them in an array MARKS.
Determine and print the maximum and minimum mark.
Program Calculate2;
{This program reads in the marks of 10 students and stores them in an array. It also
finds the maximum and minimum marks}
var
min, max, n: integer;
Marks = Array[1..10] of integer;
Begin
max := 0; min := 99999;
FOR n := 1 to 10 DO
Begin
writeln(‘Please enter a mark’);
readln(Marks[n]);
IF (Marks[n] > max) THEN
max := Marks[n];
IF (Marks[n] <main THEN
min := Marks[n];
End;
writeln(‘The maximum mark is: ‘, max);
writeln(‘The minimum mark is: ‘, min);
End.
EXERCISE #2
Consider the following array of integers:
10 7 12 8 26 19 21
1 2 3 4 5 6 7
Program Procedures;
Procedure Hello;
Begin
Writeln('Hello');
End;
{Main Program}
Begin
Hello;
End.
Eg 3.)Using Eg.2, re-write the program using procedures.
{Main Program}
Begin
Initialise;
Determine;
OutputData;
End.
EXERCISE #3
Using Exercise #2, write each part of Q4 as separate procedures and incorporate them into
a single program.