Pascal Programming 101

Program structure

Variables

Integer (-32768 to + 32767)

Real (e.g. 5.74e+03 = 5.74x103)

Char (e.g. 'A', '3', '!')

String (e.g. 'TL girl')

Boolean (e.g. TRUE, FALSE)

Basic statement

Assignment statement A := 1;
Assignment operator :=
image Variable A is like a box in computer memory

Input statements

readln(X);
read(X);
image Input value from keyboard and store in variable X

Output statements

write vs writeln

write('A');
write('B');
write('C');

writeln('A');
writeln('B');
writeln('C');
出字後,開新行

Output:
A
B
C

Output: ABC

Formatted output
writeln('ABC' :6);  Output:  ABC
wrtieln(1.2345 :6:2);  Output: 1.23

Operators

Arithmetic

, 2 + 3 gives 5

, 14 – 8 gives 6

*, 5 * 3 gives 15

/, 12 / 5 gives 2.4

div, 9 div 2 gives 4 (quotient)

mod, 9 mod 2 gives 1 (remainder)

Relational

= equal to

<> not equal to

< less than

> greater than

<= less than or equal to

>= greater than or equal to

image

image

Functions

Mathematical

Ordinal

Transfer

sqr(X), square, sqr(2.5) gives 6.25

sqrt(X), square root, sqrt(4) gives 2

abs(X), absolute value, abs(-3) gives 3

int(X), integer, int(5.67) gives 5.0

pi, 3.1415926...

odd(X), test for odd number, odd(3) gives TRUE
odd(4) gives FALSE

chr(X), return char with ASCII value X, chr(65) gives 'A'

ord('X'), return ASCII value of char 'X', ord('A') gives 65

round(X), round(4.56) gives 5

trunc(X), trunc(4.56) gives 4

Random number generator

random gives real number between 0 and 1

random(N) gives integer between 0 and N-1

Selection statement

if statement

case statement (multiple selection)

case N of
  2, 3, 5, 7: writeln('prime');
  4, 6, 8..10 : writeln('composite')
else writeln('1 is special')
end;

if mark >= 50
then writeln('Pass');

if mark >= 50
 then writeln('Pass')
 else writeln('Fail);

image

if mark >= 50
then begin
   writeln('Pass');
   writeln('Good')
   end
else writeln('Fail);

Compound statement in if statement IMG_1946

Nested if statement

if <condition 1>
 then if <condition 2>
    then <statement 1>
    else <statement 2>
 else if <condition 3>
    then <statement 3>
    else <statement 4>;

case N of
  'a', 'e', 'i', 'o', 'u' : writeln('vowels');
end;

Using assignment stmt in case stmt


case mark of
  0..49 : mark := mark + 20;
  50..90 : mark := mark + 10
end;

Iteration statement

Definite loop

(fixed number of iterations)

Indefinite loop

(number of iterations not fixed)

while ... do loop 先問後做

useful for validation, e.g. while input number is not acceptable, do ask user to input again

For ... do loop

repeat ... until loop 先做後問

useful for validation, e.g. repeat input until a positive value is entered

Count up
for count := 1 to 5 do
 write(count);
output: 12345

Count down
for count := 5 downto 1 do
 write(count);
output: 54321

Compound stmt in for loop
for count := 1 to 3 do
begin
 write('A');
 write('B')
end;
output: ABABAB

Count characters
for ch := 'A' to 'E' do
 write(ch);
output: ABCDE

click to edit

Summing up
sum := 0;
for count := 1 to 3 do
 sum := sum + count;
writeln(sum);
output: 6

click to edit

Nested for loop
for i := 1 to 2 do
for j := 1 to 3 do
 write(i, j);
output: 111213212223

Table format output
for row := 1 to 3 do
begin
 for col := 1 to 4 do
  write(row, col);
 writeln;
end;

repeat
 readln(X)
until X > 0; image

while X <= 0 do
 readln(X);
image

count := 0;
repeat
 count := count + 1;
 write(count)
until count >= 5;

count := 0;
while count < 5 do
begin
 count := count + 1;
 write(count)
end;
output: 12345

click to edit

click to edit

program myfirst;
const age = 15;
var i : integer;
begin
 { comment }
 writeln('Hello');
 writeln(age)
end.

output:
11121314
21222324
31323334

Strings

Declaration
var
 x : string[10]; {store at most 10 chars}
 y : string; { store at most 255 chars }

click to edit

Comparison


'a' > 'A'
'abc' > 'abd'
'XYZ' = 'XYZ'
'PQR' > 'PQ'
'X' <> 'Y'

click to edit

Functions

length

concat, +

copy

pos

length('abc') gives 3
length('1234') gives 4

'a' + 'b' + 'c' gives 'abc'
'X' + 'YZ' gives 'XYZ'
concat('X', 'YZ') gives 'XYZ'

Format: copy(string, start position, number of copy chars)
copy('TRUE LIGHT', 1, 4) gives 'TRUE'
copy('TRUE LIGHT', 6, 5) gives 'LIGHT'

Search for position of a char in a string
pos('E', 'TRUE LIGHT) gives 4
pos('A', 'TRUE LIGHT') gives 0

Procedures

STR

VAL

STR(123, strg);
stores '123' to strg

VAL('123', num, errorpos);
stores 123 to num, errorpos = 0 (because no error occurs)

VAL('123abc', num, errorpos);
stores 0 to num, errorpos = 4 (because error occurs at 4th char)

STR(3.1416 :4:2, strg);
stores 3.14 to strg

String as Array of characters


if S = 'TRUE LIGHT', then
S[1] = 'T'
S[4] = 'E'
S[5] = ' '


for count := 1 to length(S) do
 write( S[count] );
output: TRUE LIGHT

Arrays

Declaration


mark : array[1..10] of integer;
grade : array[1..20] of char;
gradecount : array['A' ..'E'] of integer;

var score : array[1..40] of integer;


max := score[1];
min := score[1];
for count := 2 to 40 do
 if score[count] > max then max := score[count];
 if score[count] < min then min := score[count];

click to edit

Adding up values and finding average

var score : array[1..40] of integer;


sum := 0;
for count := 1 to 40 do
 sum := sum + score[count];
average := sum/40;

click to edit

Searching an array

var name : array[1..40] of string;


found := false;
target := 'CHAN SIU LING';
for count := 1 to 40 do
 if target = name[count] then found := true;
if found then writeln('name found!');

click to edit

2D array

Declaration
var X : array[1..2, 1..3] of char;
image X[1, 2] \( \neq \) X[2, 1]

Tabular output


for row := 1 to 2 do
begin
 for col := 1 to 3 do
  write(X[row, col])
 writeln;
end;

Tabular input


for row := 1 to 2 do
begin
 for col := 1 to 3 do
  read(X[row, col])
 readln;
end;

Procedures

Declaration
Must be declared before main program


procedure <title>;
var <variables>;
begin
 <statement>
end;

Variables

Global variable

Local variable

program test2;
var X : integer; { global var }


procedure addone;
begin
 X := X + 1;
end;


begin { main program }
 X := 10;
 addone;
 writeln(X)
end.
output: 11

program test1;
var X, Y : integer; { global var }


procedure swap;
var temp : integer; { local var }
begin
 temp := X;
 X := Y;
 Y := temp
end;


begin { main program }
 X := 1; Y := 2;
 swap;
 writeln(X, Y)
end.
output: 21

Parameters

Using parameters make procedures more standalone, more modular

program test3;
var X : integer;


procedure addone(Y : integer);
begin
 Y := Y + 1;
end;


begin { main program }
 X := 10;
 addone(X);
 writeln(X)
end.
output: 10

program test4;
var X : integer;


procedure addone(var Y : integer);
begin
 Y := Y + 1;
end;


begin { main program }
 X := 10;
 addone(X);
 writeln(X)
end.
output: 11

Advantages


  1. Breaking long programs into smaller manageable parts called modules, easier to write, test, debug, maintain
  2. More efficient program development
  3. Procedures/sub programs can be re-used
  4. Facilitate division of labour in a coding team, large projects can be developed in parallel

User-defined function

function cube(N : integer) : real;
begin
 cube := N*N*N
end;


begin { main program }
 writeln( cube(2) )
end.
output: 8.0