INFORMATION TECHNOLOGY
CSEC CXC
* 1
Pascal Programming
•PASCAL is a programming language named
after the 17th century mathematician Blaise
Pascal. Pascal :
•provides a teaching language that highlights
concepts common to all computer languages
•standardizes the language in such a way that
it makes programs easy to write
•Strict rules make it difficult for the
programmer to write bad code!
* 2
Pascal Background
•Allows
programmers to
use complex data
types
(structured)
•easier to build
dynamic and
recursive data
structures such
as lists, trees
and graphs
Blaise Pascal , June 19, 1623 – August 19, 1662
* 3
Data Types -
defines a variable (data
item) in such a way as
a range of values which Data Range of values which the
the variable is capable variable is capable of storing
of storing, and it also type
Integer Whole numbers from -32768 to 32767
defines a set of
operations that are Byte The integers from 0 to 255
permissible to be Real Floating point numbers from 1E-38 to 1E+38
performed on variables Boolean Can only have the value TRUE or FALSE
of that type
Char Any character in the ASCII character set
Please note that four of these types
of data (char, shortint, word, and Shortint The integers from -128 to 127
longint) are not a part of the standard
Pascal definition but are included as Word The integers from 0 to 65535
extensions to the TURBO Pascal
compiler. Longint The integers from -2147483648 to 2147483647
* 4
Sample Pascal Program
Write a program in Pascal to display Parts of a Pascal Program:
the phrase Hello, World!
1. Program Title (Header) –
this tells the name of the program
PROGRAM HelloWorld(output); and state the actions the program
carries out. The title should not
contain spaces or start with a
BEGIN number or have in a special
character sign
writeln('Hello, World!')
END. 2. Body – this refers to the
statements to be executed by
the Compiler.
* 5
Declaration Statements
3. These statements are used to announce the variables and
constants that are to be used in the program as well as the
appropriate Data Type format they will take.
Declaring Constant/Variables
const var
Identifier1 = value; Identifier1 , Identifier2: Data Type
Identifier2 = value; Identifier3 : Data Type;
Identifier3 = value;
const
var
Name = 'Tao Yue';
age, year, grade : integer;
FirstLetter = 'a';
circumference : real;
Year = 1997;
LetterGrade : char;
pi = 3.1415926535897932;
DidYouFail : Boolean;
UsingNCSAMosaic = TRUE;
* 6
Assignment statements
Assignment Statements in programming are used to give a
value to a variable/constant. The Assignment Statement can
also change the value of a variable or constant.
How to use this statement:
variable_name := expression;
E.g.
some_real := 385.385837; carry a single value
some_real := 37573.5 * 37593 + 385.8 / 367.1;
or an arithmetic sequence
* 7
Operand Operation
div and mod only work on integers. / works on both reals and integers but will
always yield a real answer. The other operations work on both reals and
integers. When mixing integers and reals, the result will always be a real since
data loss would result otherwise. This is why Pascal uses two different
operations for division and integer division. 7 / 2 = 3.5 (real), but 7 div 2 = 3
(and 7 mod 2 = 1 since that's the remainder).
* 8
Good and Bad practice
Pascal compiler will not know if its to
Multiply or Subtract. Use parentheses.
Above is Pascal code not indented,
which makes for hard reading. Instead,
indents are used on the right.
* 9
Sample Pascal Codes
Useful for on screen output
See example on Average of 5 numbers using constants and
displaying results.
Use of read and readln
read treats input as a stream of characters, with lines separated by a
special end-of-line character. readln, on the other hand, will skip to the
next line after reading a value, by automatically moving past the next
end-of-line character:
* 10
Sample Pascal Codes
Use of the Write Statements
Used to communicate Write (‘This is our first example of output.’);
results to the outside world.
Used to print words on the This is our first example of output.
terminal screen.
* 11
Use of Write Statements
PROGRAM First;
{Demonstrates the use of Write Statements}
BEGIN
write (‘This is our first example of output.’);
END
OUTPUT ON SCREEN
This is our first example of output.
BAD CODING
PROGRAM First; BEGIN write (‘This is our first example of output’.)
* 12
With using the write statement in Pascal
Programming, the write statement when
used leaves the cursor where it is after
printing is complete.
write (‘My name’);
write (‘is Andrew’) My nameis Andrew
SPACE
USED
PROGRAM OneLine (output);
BEGIN write (‘My name’);
write (‘My name’); write (‘ is Andrew’);
write (‘is Andrew’);
END
* 13
USE OF WRITELN STATEMENT
With the use of the writeln statement for output,
the compiler returns the cursor to the
next line on screen.
PROGRAM TwoLines (output);
BEGIN
writeln (‘My name’);
writeln (‘ is Andrew’).
END.
My name
is Andrew
*
14
Use of Write and Writeln
PROGRAM Song (output); Write a program in Pascal that
displays the first stanza of the
BEGIN song “This is the land of my
birth” by Eric Donaldson. Each
write (‘This is the land’); line of output, should start on a
write (‘ of my birth!’); new line.
write (‘This is the land’);
write (‘ of my birth!’);
writeln (‘this is Jamaica, my Jamaica,’);
writeln (‘this is the land of my birth.’);
END.
This is the land of my birth!
This is the land of my birth!
this is Jamaica, my Jamaica,
Output
this is the land of my birth.
* 15
Use of Data Identifiers
•An Identifier is a name given to an item of
data in a program.
•The Identifier name once used can only
represent that one item of data when
called or used in a program.
•The Identifier name should not begin with
a number, special character or contains
special characters.
Name Age #Room Team 1 Full
* 16
Demonstration of Variable
declaration statements
PROGRAM Storing (output);
Program that takes the
{Demonstrates use of variables}; value for Width as 32’ and
Length 152’ of a Rectangle
and calculate the Area.
VAR Length, Width, Area: integer;
BEGIN
Length:= 152;
Width:= 32;
Area:= Length * Width;
writeln(‘Length = ‘, Length, ‘Width = ‘, Width, ‘Area = ‘ , Area);
END.
* Length = 152 Width = 32 Area = 4864 17
PROGRAM Storing (output);
{Demonstrates use of variables};
VAR Length, Width, Area: integer;
BEGIN Output
Length:= 15; Length = 15 Width 20 Area = 250
Length = 20 Width 20 Area = 400
Width:= 20;
Area:= Length * Width;
writeln(‘Length = ‘, Length, ‘Width = ‘, Width, ‘Area = ‘
‘Area);
Length:= 20;
Area:= Length * Width;
writeln(‘Length = ‘, Length, ‘Width = ‘, Width, ‘Area = ‘
‘Area);
END.
* 18
Demonstration of Variable
declaration statements
PROGRAM Salary (output);
Program that takes the
{Adds two salaries}; value of two salary
amounts $500.29 and
VAR Salary1, Salary2, Total: real; $500.01 and provide the
sum of both stored in a
variable Total.
BEGIN
Salary1:= 500.29;
Salary2:= 500.01;
Total:= Salary1 + Salary2;
writeln (‘Salary Week 1 =$’, Salary1, ‘Salary
Week 2 =$‘, Salary2, ‘Total Salary =$’, Total);
END.
Salary Week 1 =$500.29, Salary Week 2 = $500.01,
Total Salary = $1000.30
* 19
Try this~!
Write a program code in Pascal to take a time of .3
seconds and velocity of 24 and calculate the distance by
use of the formula Time travelled times Velocity and
output this distance on a new line.
PROGRAM Distance (output); Distance Travelled = 7.2
VAR Time, Velocity, Distance: real;
BEGIN
Time:= .3;
Velocity:= 24;
Distance:= Time * Velocity;
writeln (‘Distance travelled is =‘, Distance);
END.
* 20
Use of screen prompts
Write a program in Pascal to accept the Length and Width of a piece of
lumber along with the price and provide the cost where Cost = Length x
Width x Price. An output should be provided.
PROGRAM Area (input, output);
VAR Length, Width: integer;
Cost, Price: real;
BEGIN
writeln (‘Type in Length, Width and Price’);
readln (Length, Width, Price);
Cost:= Length * Width * Price;
writeln (‘The cost is:= ‘, Cost);
END.
* 21
Constant – Use of!
•Constants represents a value of data that remains fixed is not
designed or expected to change. E.g. the number of days in
a week could be set to 7 as there can be only 7 days in 1
week.
•The number of ounces in a pound of flour will always be 16
ounces represent 1 pound.
* 22
Write a program in Pascal that calculates the volume of a shape provided with
the Pi as 3.14159 and Height being 6. The program should accept Radius and
calculate the volume where Volume =
PROGRAM AreaVolume (input, output);
CONST Pi = 3.14159; H = 6;
VAR Volume, R: real;
BEGIN
writeln (‘Type in the radius.’);
readln (R);
Volume:= Pi * R * R * H;
writeln (‘Volume = ‘, Volume);
END.
* 23