genjava / notes / html-colours
The following is an online version of my pascal hint book.
Every pascal program has the following type of heading :
program progname( devices/files to be used );
while
while <boolean = true> do
CODE
Checks the boolean is true, then does CODE, then
checks again. Continues until the boolean is false.
for
for i := a to b do
CODE
Iterates i from a to b,
doing CODE each time.
for i := b downto a do
CODE
Iterates i downwards, from b to
a, doing CODE each time.
Do not change i in CODE. Do not use
i outside CODE.
repeat
repeat
CODE
until <boolean = true>
Repeatedly does CODE until the boolean is true. The
same as a while loop, but the boolean is inverted and it is not
checked until after CODE is done the first time. The
CODE block does not need to be inside a
begin and end statement.
if
if <boolean = true> then
statement1
else statement2
If the boolean is true then statement1 is executed.
If the boolean is false then statement2 is executed.
case
case (ordinal variable) of
member1 of ordinal variable set : statement1
member2 of ordinal variable set : statement2
......
end
Used to do many if statements at once. There is no
default option inside the case to handle anything else. The following
could handle that.
if x in [1,2,3,4] then
case x of
1 : ...
2 : ...
3 : ...
4 : ...
end
else println('Not 1-4.')
Subroutines are pieces of code which may be used again and again. Pascal has two types of subroutines, functions and procedures. Subroutines may have data passed to them in the form of arguments. Arguments are enclosed in parentheses (brackets) and it is important to ensure that the arguments are in the same order in the place where you define the subroutine and where you call the subroutine.
function
Functions return a single value.
function function_name( argument1 : type; argument2 : type; ... ):function_return_type;
begin
......
function_name := ....
end;
function_name is both the returned variable and the
name of the function. Usage of a function is as follows :
x := function_name( argument1, argument2, ... )
The variable x must be of the same type as
function_return_type.
Stylistically, functions are used for mathematical operations. Do
not use the read or write procedures in a
function. Output/input does not behave normally inside a function.
procedure
Procedures do not return a value.
procedure procedure_name( var variable_argument : type; value_argument : type; ...);
begin
....
end;
Variables preceded by the var keyword are linked to
the passed arguments. They change in the procedure and in the scope
of code that called them. To call a procedure :
procedure_name( arguments )
Output and Input are allowed in procedures. You don't have to pass
var arguments, value arguments or any at all. Passing a
variable parameter is known as calling by reference,
whilst passing a value argument is known as calling by
value. If calling a procedure with no arguments use :
procedure_name
ie) Don`t use the brackets.
Subroutines must be defined before anywhere in the program trys to
use them. That is the code for a subroutine must be above all the
positions that try to call the subroutine. Sometimes that is
impossible however and it is possible to declare a
subroutine, implying that it will be defined later. To do this the
forward keyword is used, as in the following example.
procedure bob(); forward;
function jim:integer;
begin
...
bob;
...
end;
procedure bob;
begin
...
x := jim;
...
end;
|
|
|
|
|
|
|
|
|
|
|
|
Note The real numbers are stored bit-wise. This means two large numbers which may differ by a small amount may appear as the same number.
| ||||||||||||||||
| ||||||||||||||||
| ||||||||||||||||
|
To declare a constant for a program :
const
constname = constvalue;
To declare a type of variables :
type
typename = type in terms of basic data-types and types declared thus far;
To declare a variable :
var
varname : vartype;
arrays
Type defined as :
type
arrayname : array[index-type] of data-type;
index-type must be ordinal.
data-type must be declared before the array is.
Setting an element of an array. For example the first element :
arrayname[1] := 5;
Getting the value stored in an element of an array. For example that stored in the fifth element :
i := arrayname[5]
arrays of arrays
It is possible to create an array in which every element is an array. If wanted every element in that sub-array could be an array. Ad-infinitum. It would be done like this :
arrayofarrays : array[index-type] of arrayname;
Using the arrayname declaration above.
To access the elements use the following form :
arrayofarrays[1][1]
The array of arrays could have been declared like this :
arrayofarrays : array[index-type] of array[index-type] of data-type;
n-dimensional array
Another way to handle arrays is by declaring them in the following manner :
2darray : array[index-type,index-type2] of data-type;
This would be accessed like this :
2darray[1,1]
You may do as many dimensions as you like.
records
Type defined as :
type
recordname = record
type declarations;
ie) field1 : integer;
end;
The declare the record :
var
record1 : recordname;
record2 : recordname;
You cannot compare records directly. ie) can`t do :
if( record1 = record2 )
To get something 'out' of a record, use the dot operator.
record1.field1;
Records may be nested. If they are then they their fields are obtained by a tree-like structure.
record1.subrecord1.field1;
Here, record1 would have had to be defined after
subrecord1.
It is possible to have conditional fields/records.
condrec = record
field1 : type;
field2 : type;
case field3 : type of
entry1 : (field31 : type);
entry2 : (field32 : type);
entry3 : (field33 : type,
field34 : type);
end;
Note how the case part has no end. The case part, or Variant part must be after the fixed part of the record and must be just before the record's end.
The with statement
This is used to make records easier to use, eg)
with record1 do
begin
read( field1 ); { Instead of record1.field1 }
read( record2.field1 );
write( field2 ); { Instead of record1.field2 }
end;