Professional Documents
Culture Documents
Lesson 10 INTRODUCTION TO THE PASCAL LANGUAGE Handout
Lesson 10 INTRODUCTION TO THE PASCAL LANGUAGE Handout
Problem-solving phase
1. Define the problem
Implementation phase
1. Translate the algorithm into a specific programming language
The Program heading is a single statement beginning with the word program. The heading
assigns a name to the program.
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;
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.
Developer: Student
Below is a table comparing the keywords of a pseudocode versus the keywords used in Pascal
Code.
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)
Boolean Operators
(used in If statements 2= 2
and comparisons)
2<4
=
4>2
<
4<>2
>
<>
<=
>=
Assignment symbols
PASCAL PROGRAM HANDOUT
Assigns the value on the := Assigns the value on the right to Variable
right to Variable on the left on the left
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.)
+ Addition 2+2=4
– Subtraction 4–2=2
/ Division 8/4=2
* Multiplication 4 * 8 = 32
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)
Using the keyword Writeln it would appear on the screen like this:-
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
Begin
Start of instruction statements
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
Syntax 1
If (condition) then
One Statement;
IF-then-else Structures
Syntax 1
if( condition ) Then
One statement [no semicolon here]
else
One statement;
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;
If (condition) Then
Begin
One or more statements;
End
Else
If (condition) Then
Begin
One or more statements;
End;