Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

PASCAL PROGRAM HANDOUT

Problem-solving phase
1. Define the problem

2. Find a solution (s) to the problem

3. Evaluate alternative solution

4. Develop and represent the most efficient solution as an algorithm

5. Test the algorithm for correctness

Implementation phase
1. Translate the algorithm into a specific programming language

2. Execute the program on the computer

3. Maintain the program

INTRODUCTION TO THE PASCAL LANGUAGE


You are going to learn how to write a program using the Pascal Language and complete the first
step of the implementation phase (Translate the algorithm into a specific programming
language).

STRUCTURE OF A PASCAL PROGRAM

A Pascal program has three distinct parts:


1. The program heading
2. The program block
3. The program terminator (a period)

The Program heading is a single statement beginning with the word program. The heading
assigns a name to the program.

Eg. program sba_2018;

The program block is the body of the program. The block is divided into two distinct parts:

1. The constant and variable declaration section where all the constants and variables data
structures used by the program are defined (Shown in program as const and var).
Const interestrate = 0.10;

Var fname : string;

2. The statement section is where all the action statements of the program are specified. The
statement section is encapsulated within begin and end statements.

Begin
Writeln (‘Please enter first name’);
Readln (fname)
Writeln ( ‘Hello’ , ‘ ‘, fname , ‘nice meeting you’);
End.

Begin and end are examples of keywords used in Pascal. Keywords (or reserved words) are
words that have special meaning in Pascal and can only be used in the predefined context, that is,
PASCAL PROGRAM HANDOUT

they cannot be used as variable names or in any other context. Other key words are: program,
type, var, const, read, write, readln, and writeln.

When you are adding comments to your program they are included between parenthesis { } or
between (* *) and can span multiple lines.

Eg {Date: November 21, 2018

Developer: Student

Purpose: This program will output the pattern of a Christmas tree. }

Below is a table comparing the keywords of a pseudocode versus the keywords used in Pascal
Code.

PSEUDOCODE KEYWORDS PASCAL KEYWORDS

The word Algorithm indicates the name of the The word program signals the name of the
algorithm program

1) The word Start is used to begin the body The word Begin is used to start the body
algorithm Pascal program

2) The words Read/Accept is used to input The words Read and Readln are used to input
values values

OPERATORS SAME

Addition ( +) 2 +2 = 4

Subtraction ( - ) 4 – 2= 2

Multiplication ( * ) 4*2=8

Division ( / )

DIVISION (

Division (MOD)

(mod is used when


only the remainder is
required

Boolean Operators

(used in If statements 2= 2
and comparisons)
2<4
=
4>2
<
4<>2
>

<>

<=

>=
Assignment symbols
PASCAL PROGRAM HANDOUT

= Assigns Values to constants = Assigns Values to constants

Assigns the value on the := Assigns the value on the right to Variable
right to Variable on the left on the left

5) To display results/messages: Print, Display, To display results: Write or Writeln


Output

6) To end an algorithm the word Stop is used To end a Pascal program the word End
followed by a full stop is used. (End.)

Operator Name and description Example

+ Addition 2+2=4

– Subtraction 4–2=2

/ Division 8/4=2

* Multiplication 4 * 8 = 32

Exponentiation: When one number increases


2 ^ 4 = 16 (2 * 2 * 2 * 2 =
^ exponentially (the number of times) to another. The
16, or 24)
repeated multiplication of a number by itself.

Modulus: The remainder that is left over when a


16 MOD 3 = 1 (16 / 3 =
MOD number is divided by another. Some programming
5, with 1 left over)
languages will use the % symbol for MOD.

Integer division: Used to find the quotient (integer 100 DIV 3 = 33 (actual
DIV number before the decimal point) after division. Python value 33.333333333
uses // for this. repeating)

Some common mathematical operations used in programming include:

KEY DISTINCTIONS IN PASCAL


PASCAL PROGRAM HANDOUT

 The difference between READ and READLN


There is no difference between read and readln when working with numeric values such as 2, 6,
and 10. The difference applies when working with string values or characters on different lines
such as ‘A’, ‘B’ and ‘C’. When the computer reads the first letter (‘A’) if you use the keyword
READ the program will not move to the next line to read B and C. So the keyword READLN is
used to read in such values.

 The difference between WRITE and WRITELN


The keyword Write outputs text or values to the screen in a single line leaving the cursor
positioned at the end of line. On the other hand Writeln which means write line, outputs text or
values to the screen in more than one line leaving the cursor at the beginning of the next line,
instead of at the end of the current line.
For example, if you were outputting the sentence “My First Program”.

Using the Write keyword it would look like this:-

My first program. __ The Cursor is


positioned at the
end of the line.

Using the keyword Writeln it would appear on the screen like this:-

My The words are displayed on


first separate lines and cursor is
program.Pseudocode into Pascal Code
Translating positioned at the beginning
__ of the next line.

 The first step in translating an algorithm into Pascal code is to make a list of all the
variables used in the algorithm and determine their data type (i.e. the type of values that
each variable is to store).
 Translate the pseudocode into Pascal code based on the Pascal structure you were given
above.

Example 1
Write an algorithm to read three numbers and find the average of the numbers and output the
average of the numbers.

Pseudocode Version

Pseudocode Algorithm Average_of_Numbers


Date
Developer
This algorithm finds the average of three numbers.
Declare num1, num2, num 3 as integer
Average as real
Start
Read num1, num2, num3
Average (num1 + num2 + num3)/3
Print “The average is”, Average
Stop Program
Heading
Pascal Code
PASCAL PROGRAM HANDOUT

Program Average_of_Numbers ; Comments shown in


{ Date : Nov 21, 2018 parenthesis.
Developer : K White
This algorithm finds the average of three numbers}
var num1, num2, num3: integer;
Average: real; Declaring your variables
and their data type

Begin
Start of instruction statements

Readln (num1, num2, num3);


Average := (num1 + num2 + num3)/3; Program Block
Writeln(‘The average is:’, Average);

End.
End of Program/ Terminator

Please Note:
 All program statements and lines are terminated with a semi-colon (;), except the ‘begin’
and ‘end’ keywords. Program statements preceding an end statement do not require a
semi-colon (optional).

 When outputting text to the screen as opposed to pseudocode where double quotations
are used, in Pascal single quotations are used. For example: Writeln(‘I Love to
Program’);

IF IN PASCAL STRUCURES
PASCAL PROGRAM HANDOUT

IF with Null Else structures. IF-then

Syntax 1
If (condition) then
One Statement;

Syntax 2 (Recommended to be used)


If (condition) Then
Begin
One or more statements;
End;

IF-then-else Structures

Syntax 1
if( condition ) Then
One statement [no semicolon here]
else
One statement;

Syntax 2 (Recommended to be used)

If (condition) Then
Begin
One or more statements;
End
Else
Begin
One or more statements;
End;

IF-then-else-IF/Nested IF Structures

Syntax 1
if( condition ) Then
One statement [no semicolon here]
Else
if( condition ) Then
One statement;

Syntax 2 (Recommended to be used)

If (condition) Then
Begin
One or more statements;
End
Else
If (condition) Then
Begin
One or more statements;
End;

You might also like