Pascal Lecture notes –
INPUT
statements
READ (X); (* is equivalent to saying *) READ (input, X);
READLN (X) (* is equivalent to
saying *) READLN (input, X);
READ (file, X) (* means read from the file known as file *)
OUTPUT
statements
WRITE (X); (* is equivalent to saying *) WRITE (output, X);
WRITELN (X); (* is equivalent to
saying *) WRITELN (output, X);
NOTE: you will need to distinguish between the
internal and the external file names.
The one used in the READ, READLN, WRITE, and WRITELN statements is the
internal name (variable name) not the diskfile (string) name.
ITERATION
statements
FOR loop is a
counted loop. It executes a fixed number
of times
FOR loopControlVariable := intialValue
TO endingValue DO
SingleStatement;
FOR loopControlVariable := intialValue
TO endingValue DO
BEGIN MultipleStatements;
END;
FOR loopControlVariable := initialValue DOWNTO endingValue DO
SingleStatement;
WHILE loop is
executes until a condition is met. It
will not be executed at all if the condition is not true on entry to the
loop. (Pre-test loop)
WHILE (true) DO
Do
this one thing;
WHILE (true)
DO
BEGIN
END;
(* the begin end
pair of reserved words makes a group of statements equivalent syntactically to
a single statement *)
REPEAT …
UNTIL loop executes until a condition is
met. It always executes at least once because it’s a post test loop.
ASSIGNMENT statement
x:=y;
NOTE: in Pascal, the semi-colon is a statement
separator not a statement terminator.
SELECTION statements
IF (condition) THEN
singleThingToDo;
IF (condition) THEN
BEGIN
multipleThingsToDo
END;
IF
statements can be nested.
IF (condition) THEN
singleThingToDo (* NEVER
EVER A SEMICOLON BEFORE AN ELSE *)
ELSE
singleThingToDo;
CASE is a
multi-alternative selection statement
CASE identifier OF
Value : action; (* action can be single or multiple statements *)
Value2 :
action2;
Value3..Value5 :
action3;
Value4, value7, value12 :
action4;
END; (* of case
statement *)
Things beyond the 5 basic
statements
It’s useful to
know how to comment in a language
(* *) shows the delimiters for Pascal comments
{ }
Pascal is NOT case
sensitive
Pascal only guarantees identifiers are unique if they are different from
all other identifiers in the first 8 characters
Pascal has both reserved words (35 or 36) and predefined terms
Syntax
File
handling: UseFiles2.pas
Sub-programs
Procedures
Functions
Parameter Passing modes
Var parameters - pass by address (reference)
Value parameters - copy a value in
Data types
Below is a diagram of the data types in the