-
Try to declare all variables. A simple convention is to give the name of
the variable followed by a colon followed by its type. Examples:
x : integer
p : node pointer
-
Functions: Generally, we will assume that functions are call by value.
If a parameter is intended to be call by reference then that should be
indicated. The arguments should have their types indicated and be separated
by commas. The return value if any should be indicated. Examples:
concatenate(p, q : list pointer) : list pointer
find(x : integer, p reference tree pointer) : tree pointer
sum(v[] : integer array; num : integer) : integer
-
Sequences of statements: Separate by with semicolons and group using curly
brackets, { }, when needed.
-
Conditionals: if "condition" then "statement" or if "condition" then "statement1"
else "statement2" .
-
Case statement: the case statement is quite handy. The case can follow
the following syntax:
case
condition1 : statement1 ;
condition2 : statement2 ;
.
.
endcase
-
While loop: while "condition" do "statement".
-
Repeat loop: repeat "statement" until "condition".
-
for loop: for i = "start index" to "end index" do "statement". Occationally,
there may be a need for stepping by more than 1, in which case, we have
for i = "start index" to "end index" by "step" do "statement".
-
Records and pointers: If p is a pointer to a record with field x, the field
can be accessed using the notation p.x. Generally it will be clear from
context what are pointers and what are not.
-
Memory management: We assume a simple memory management scheme where an
object of any type can be obtained by using the term "new", usually a pointer
is returned. Don't bother to destroy objects unless it is required. We'll
just assume an infinite amount of new memory.
-
Arrays: to obtain the i-th item from array A, simply write A[i]. An array
used as function parameter can be assumed to be by reference. Declaring
an array can be done by specifying its range. For example:
A[1..N] : integer array
-
Assignment: use := (this is nicer than = but we won't penalize
if you use = and the context is evident )
-
Boolean operators: use and, or, not, implies (or their mathematical
symbols)
-
Equals operator: use = for any simple objects such as pointers and integers
(you can use = = but then be sure to be consistent with the Assignment
operator).
-
Arithmetic operators: use +, -, *, ^, /, mod for integer addition, subtraction,
multiplication, exponentiation, division (yields the quotient), and division
(yields the remainder).