Please enable JavaScript.
Coggle requires JavaScript to display documents.
Pascal Programming 101 (Variables (Integer (-32768 to + 32767), Real (e.g.…
- 
- Pascal Programming 101
- 
- Variables
- 
- 
- 
- Char (e.g. 'A', '3', '!') 
 
- 
- 
- Boolean (e.g. TRUE, FALSE) 
 
 
- 
- Functions
- 
- Mathematical
- 
- 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 
 
- 
 
- 
- Ordinal
- 
- odd(X), test for odd number, odd(3) gives TRUE
 odd(4) gives FALSE
 
 
 
- 
- Transfer
- 
- 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 
 
 
- 
- 
- User-defined function
- 
- function cube(N : integer) : real;
 begin
 cube := N*N*N
 end;
 begin { main program }
 writeln( cube(2) )
 end.
 output: 8.0
 
 
 
 
- 
- Strings
- 
- 
- 
- Functions
- 
- 
- concat, + 
- 
- 'a' + 'b' + 'c' gives 'abc'
 'X' + 'YZ' gives 'XYZ'
 concat('X', 'YZ') gives 'XYZ'
 
 
 
- 
- copy 
- 
- Format: copy(string, start position, number of copy chars)
 copy('TRUE LIGHT', 1, 4) gives 'TRUE'
 copy('TRUE LIGHT', 6, 5) gives 'LIGHT'
 
 
 
- 
- pos 
- 
- Search for position of a char in a string
 pos('E', 'TRUE LIGHT) gives 4
 pos('A', 'TRUE LIGHT') gives 0
 
 
 
 
- 
- Procedures
- 
- STR 
- 
- STR(123, strg);
 stores '123' to strg
 
 
- 
- STR(3.1416 :4:2, strg);
 stores 3.14 to strg
 
 
 
- 
- VAL 
- 
- 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)
 
 
 
 
- 
- 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];
 
- 
 
 
- 
- 
- 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!');
 
- 
 
 
- 
- 2D array
- 
- Declaration
 var  X : array[1..2, 1..3] of char;
  X[1, 2] \( \neq  \) X[2, 1] 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;
 
 
 
- 
- Operators
- 
- Arithmetic
- 
- 
- 
- 
- 
- div, 9 div 2 gives 4 (quotient) 
 
- 
- mod, 9 mod 2 gives 1 (remainder) 
 
 
- 
- 
- 
 
- 
- Procedures
- 
- Declaration
 Must be declared before main program
 
 procedure <title>;
 var <variables>;
 begin
 <statement>
 end;
 
- 
- Variables
- 
- Global 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
 
 
 
- 
- Local variable
- 
- 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
 
 
 
 
- 
- ParametersUsing 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- 
-  Breaking long programs into smaller manageable parts called modules, easier to write, test, debug, maintain
-  More efficient program development
-  Procedures/sub programs can be re-used
-  Facilitate division of labour in a coding team, large projects can be developed in parallel
 
 
 
 
- 
- Basic statement
- 
- Assignment statement A := 1;
 Assignment operator :=
  Variable A is like a box in computer memory Variable A is like a box in computer memory
 
 
- 
- Input statementsreadln(X);
 read(X);
  Input value from keyboard and store in variable X Input value from keyboard and store in variable X
 
- 
 
- 
- Selection statement
- 
- if statement
- 
- 
- 
- 
- 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>;
 
- 
 
 
 
- 
 
- 
-