0% found this document useful (0 votes)
41 views

Pascal Programming 101

This document discusses the advantages of procedures in programming. It lists the advantages as: 1. Breaking long programs into smaller manageable parts called modules, which are easier to write, test, debug, and maintain. 2. More efficient program development. 3. Procedures/sub programs can be reused. It also provides a simple example of a procedure that swaps the values of two variables.

Uploaded by

ProxyYt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Pascal Programming 101

This document discusses the advantages of procedures in programming. It lists the advantages as: 1. Breaking long programs into smaller manageable parts called modules, which are easier to write, test, debug, and maintain. 2. More efficient program development. 3. Procedures/sub programs can be reused. It also provides a simple example of a procedure that swaps the values of two variables.

Uploaded by

ProxyYt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Advantages program test1;

var X, Y : integer; { global var }


Declaration 1. Breaking long programs into smaller manageable
var X : array[1..2, 1..3] of char; parts called modules, easier to write, test, debug, procedure swap;
maintain var temp : integer; { local var }
begin
X[1, 2] X[2, 1]
Procedures 2. More efficient program development
3. Procedures/sub programs can be re-used  temp := X;
4. Facilitate division of labour in a coding team, large Local variable  X := Y;
projects can be developed in parallel  Y := temp
Tabular output end;

for row := 1 to 2 do begin { main program }


begin  X := 1; Y := 2;
 for col := 1 to 3 do Declaration  swap;
  write(X[row, col]) Must be declared before main program  writeln(X, Y)
 writeln; end.
end; procedure <title>; output: 21
var name : array[1..40] of string; Variables
2D array var <variables>;
Tabular input begin
found := false;  <statement> program test2;
target := 'CHAN SIU LING'; end; program test3; var X : integer; { global var }
for count := 1 to 40 do for row := 1 to 2 do
begin var X : integer;
 if target = name[count] then found := true; Searching an procedure addone;
if found then writeln('name found!'); array  for col := 1 to 3 do begin
procedure addone(Y : integer);
  read(X[row, col])  X := X + 1;
begin
 readln; end;
 Y := Y + 1;
end; Call by value end;
var score : array[1..40] of integer; Global variable begin { main program }
begin { main program }  X := 10;
sum := 0;  addone;
for count := 1 to 40 do  X := 10;
 writeln(X)
 sum := sum + score[count]; Arrays  addone(X);
 writeln(X) end.
average := sum/40; output: 11
end. program test4;
Parameters output: 10 var X : integer;
Adding up
Using parameters make
var score : array[1..40] of integer; values and procedures more standalone, more procedure addone(var Y : integer);
finding average modular begin
max := score[1];  Y := Y + 1;
min := score[1]; end;
for count := 2 to 40 do
 if score[count] > max then max := score[count]; Call by reference begin { main program }
 if score[count] < min then min := score[count];  X := 10;
 addone(X);
 writeln(X)
Finding maximum end.
and minimum Declaration
values
output: 11 Program program myfirst;
const age = 15;
mark : array[1..10] of integer;
grade : array[1..20] of char; structure var i : integer;
begin
gradecount : array['A' ..'E'] of
VAL('123', num, errorpos); VAL  { comment }

Pascal
integer;  writeln('Hello');
stores 123 to num, errorpos = 0
(because no error occurs)  writeln(age)
end.

VAL('123abc', num, errorpos);


stores 0 to num, errorpos = 4 (because
Programming
error occurs at 4th char) Procedures
101 Integer (-32768 to + 32767)

Real (e.g. 5.74e+03 = 5.74x


length STR(123, strg);
length('abc') gives 3 Char (e.g. 'A', '3', '!')
length('1234') gives 4 stores '123' to strg
Variables String (e.g. 'TL girl')
STR(3.1416 :4:2, strg); STR
'a' + 'b' + 'c' gives 'abc' concat, + stores 3.14 to strg Boolean (e.g. TRUE, FALSE)
'X' + 'YZ' gives 'XYZ'
concat('X', 'YZ') gives 'XYZ'

Assignment statement A := 1;
Format: copy(string, start position, number of copy chars) Functions Assignment operator :=
copy('TRUE LIGHT', 1, 4) gives 'TRUE'
copy('TRUE LIGHT', 6, 5) gives 'LIGHT' copy
Variable A is like a box in computer

Search for position of a char in a string


pos('E', 'TRUE LIGHT) gives 4
pos('A', 'TRUE LIGHT') gives 0 pos memory
Comparison Strings Basic statement write('A');
write('B');
'a' > 'A' write('C');
Count characters Count down Count up 'abc' > 'abd' Output: ABC
for ch := 'A' to 'E' do for count := 5 downto 1 do for count := 1 to 5 do String as Array of characters
'XYZ' = 'XYZ'
 write(ch);  write(count);  write(count); 'PQR' > 'PQ' Output statements Output:
output: ABCDE output: 54321 output: 12345 'X' <> 'Y' if S = 'TRUE LIGHT', then A
write vs writeln
S[1] = 'T' B
S[4] = 'E'
Precedence 優先次序 writeln('A'); C
Declaration writeln('B');
For ... do loop var
S[5] = ' '
writeln('C');
 x : string[10]; {store at most 10 chars} 出字後,開新行
for count := 1 to length(S) do
 y : string; { store at most 255 chars }  write( S[count] );
Summing up Compound stmt in for loop
for count := 1 to 3 do output: TRUE LIGHT
sum := 0;
begin Formatted output
for count := 1 to 3 do
 sum := sum + count;
 write('A');
 write('B')
Definite loop > greater than Operators +, 2 + 3 gives 5 Input statements
readln(X);
writeln('ABC' :6);  Output:  ABC
wrtieln(1.2345 :6:2);  Output: 1.23
writeln(sum); (fixed number of iterations) –, 14 – 8 gives 6 read(X);
end; >= greater than or equal to Arithmetic
output: 6 output: ABABAB *, 5 * 3 gives 15
< less than
Input value from keyboard and store in variable
Nested for loop <= less than or equal to /, 12 / 5 gives 2.4
Table format output for i := 1 to 2 do
for row := 1 to 3 do for j := 1 to 3 do = equal to div, 9 div 2 gives 4
begin  write(i, j); Relational (quotient)
output:  for col := 1 to 4 do X
output: 111213212223
11121314   write(row, col); <> not equal to mod, 9 mod 2 gives 1
21222324  writeln; (remainder)
31323334 end;
repeat
 readln(X) Maths expressions
until X > 0;

count := 0;
repeat function cube(N : integer) : real;
 count := count + 1; begin
 write(count)
repeat ... until loop 先做後問  cube := N*N*N
until count >= 5;
useful for validation, e.g. repeat input until a
positive value is entered
Iteration statement User-defined function end;

begin { main program }


 writeln( cube(2) )
end.
while X <= 0 do output: 8.0
count := 0;  readln(X);
while count < 5 do
begin pi, 3.1415926...
 count := count + 1;
 write(count) Indefinite loop
end; (number of iterations not fixed) abs(X), absolute value, abs(-3) gives 3
output: 12345
if mark >= 50
then writeln('Pass');
Selection statement Functions Mathematical
sqr(X), square, sqr(2.5) gives 6.25

while ... do loop 先問後做 sqrt(X), square root, sqrt(4) gives 2


useful for validation, e.g. while input number is if mark >= 50
not acceptable, do ask user to input again  then writeln('Pass') int(X), integer, int(5.67) gives 5.0
 else writeln('Fail);

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


case N of odd(4) gives FALSE
Compound statement in if statement   'a', 'e', 'i', 'o', 'u' : writeln('vowels'); Ordinal
end;
if statement
case statement Using assignment stmt in case chr(X), return char with ASCII value X, chr(65) gives 'A'
if mark >= 50 stmt
then begin (multiple selection) Transfer ord('X'), return ASCII value of char 'X', ord('A') gives 65
   writeln('Pass'); case mark of
   writeln('Good')   0..49 : mark := mark + 20 ; trunc(X), trunc(4.56) gives 4
    end   50..90 : mark := mark + 10
else writeln('Fail); end; round(X), round(4.56) gives 5

case N of
  2, 3, 5, 7: writeln('prime');
  4, 6, 8..10 : writeln('composite')
random gives real number between 0 and 1
Nested if statement else writeln('1 is special')
Nested If if <condition 1> end;
 then if <condition 2> random(N) gives integer between 0 and N-1
     then <statement 1>
     else <statement 2> Random number random(B - A + 1) + A gives an integer
 else if <condition 3> between A and B inclusively
generator
     then <statement 3>
     else <statement 4>;

You might also like