Lecture 9 - September
25, 2007
graded Core exercise was returned.
in the 2pm class, we looked at all
of the Alice
programs that students wrote
graded text book homework was
returned
graded Second FORTRAN was returned
and some elements were discussed.
- how to
simulate an IF (<condition>)
THEN <statements> ELSE
<statement> END IF using GO
TO statements was illustrated
- how to
simulate a WHILE (<condition>)
DO <statements> END DO using GO TO statements was illustrated
green
card with Pascal sample
program and other Pascal information was handed out and discussed.
- Attention
was paid to the need for including (input, output) in the program
header statement if any input is to come from the keyboard and any output
is to go to the screen.
- Constants (e.g. const)
are to be declared before types (e.g.
type) are to be declared before
variables (e.g. var)
.
- Procedures
and functions are to be included in the main program and must precede the begin statement of the main program and
follow the var declarations of the main
program..
- Variables
declared in sub-programs (i.e. procedure
and function ) are local to the sub-programs (i.e. are not known
by the main program)
- In
Pascal, variables declared in the main program are accessible to the
sub-programs unless the
sub-programs contain variables of the same name.
- while not (eof) is
identical to while not (eof(input)) and is testing for the end of the data
stream coming from the keyboard.
The user can signal this on a PC by hitting <ctrl-z>. On a linux
box, type <ctrl-d>.
- while not (eoln) is
identical to while not (eoln(input)) and is testing for the end of the
line of data coming from the keyboard.
The user can signal this by hitting the enter key.
- the reserved words (on the back of the green card) are
words that may not be used as identifiers
in Pascal programs: not as a const, not as a var, not as a type, not as a function name,
not as a procedure name,
not as a program name.
- Disk
files used in a program should always be closed before the program ends.
- A ^ (e.g. circumflex) can be typed
for the up-arrow used on the green sheet in relation to pointer variables
- Although
it is possible to declare a type
to be a file of real or a file of integer or a file
of <component-type> we
will always use the pre-defined type
textfile when we declare our file
variables (e.g. var infile
: textfile; ) and although it is possible to use
get and put with textfiles, we will not do so.
We discussed what the output from FirstPascal should look like and what should go to the screen
and what should go to a file.
We looked at parts of a Pascal
program using files. See the code below. Come to class prepared to answer questions
about the meaning of various lines of the code.
You will be given a chance to ask questions before you are questioned.
program
useFiles (input, output, infile,
outfile);
(* type declarations precede
variable declarations *)
(* the word type only appears once
even if multiple types are declared *)
type
aRecord =
record (* record (heterogeneous data structure
declaration *)
name
: String;
age
: integer;
average
: real;
end;
anArray =
array [1..5] of aRecord; (*
array (homogeneous data structure) *)
(* variable declarations follow
type declarations *)
(* word VAR only appears once even
if multiple variables declared *)
var (* omitting this gives strange error message:
begin expected *)
infile, outfile : textfile;
infileName,
outfileName : String;
singleRecord : aRecord;
i,j :
integer;
myArray : anArray;
(* subprograms,
if any -- and there should be in here *)
(* what follows
is part of a main program starting with begin *)
begin
write
(' please enter the name of your input file and hit return ');
readln ( infilename);
write
(' please enter the name of your output file and hit return ');
readln (outfilename);
assign
(infile, infileName);
assign
(outfile, outfileName);
reset
(infile);
rewrite
(outfile);
(* more code would be
here *)
close
(infile);
close
(outfile);
(* more code would be here *)
end.