Introduction to PASCAL / PDF


Introduction to PASCAL / PDF




Introduction to PASCAL / PDF






Table of Contents



Programming in PASCAL 

Programs and Data.
PASCAL and Programs ...
Structured Programs.............
The Program Heading ..........
The var Segment .............
The Instructions Segment ...........
The Borland PASCAL Environment ......
The Borland PASCAL Environment....
Your Work File ..........
The Edit Command .....................
The Compile Command ...................
The Run Command ..............
The Quit Command.....
Exercises ...

The PASCAL Language...............................................................................................12
Variable Declaration within PASCAL..........................................................................12
Standard Data Types Within PASCAL ...........................................................12
Borland PASCAL Types.................................................................................13
Data Within the Program Text .......................................................................13
Comments......................................................................................................14
Assignments ................................................................................................................15
Operands........................................................................................................15
Operators .......................................................................................................15
Standard Procedures.......................................................................................17
Defining Your Own Types - the type segment.................................................17
Exercises ........................................................................................................17
Syntax..........................................................................................................................18
ii
Statements......................................................................................................18
The Flow of Control Within a Program ........................................................................18
The IF - THEN - ELSE construction...............................................................18
The REPEAT - UNTIL construction...............................................................19
The WHILE - DO Construction......................................................................20
The FOR - DO Construction...........................................................................21
Exercises ........................................................................................................22
Procedures and Functions.............................................................................................22
Local and Global Variables ............................................................................23
Simple Parameters..........................................................................................24
Exercises ........................................................................................................25
Returning Results via Parameters ...................................................................26
Functions .......................................................................................................27
Recursion .......................................................................................................27
Constants .......................................................................................................29
Arrays..........................................................................................................................29
Exercises ........................................................................................................30
Sorting with arrays .........................................................................................30
More than one dimension arrays.....................................................................32
More than two dimensions..............................................................................32
Packed Arrays ................................................................................................32
Exercises ........................................................................................................33
File Handling in Pascal ................................................................................................34
Assigning a File .............................................................................................34
Beginning Data Transfer ................................................................................35
Reset ..............................................................................................................35
Rewrite...........................................................................................................35
Using Files with Read and Write ....................................................................35
Closing the File ..............................................................................................36
Exercises ........................................................................................................36
The Case Construction .................................................................................................36
Records........................................................................................................................37
Exercises ........................................................................................................39
Pointers........................................................................................................................39
Memory Allocation.........................................................................................41
Lists and Pointers ...........................................................................................41
Exercises ........................................................................................................42
Variant Records..............................................................................................42
Sets..............................................................................................................................44
Prettier Printing ...........................................................................................................46
Of Numbers and Accuracy..............................................................................48
The GOTO statement...................................................................................................48
Goto Restrictions............................................................................................49
Goto Philosophy .............................................................................................49
Forward Declarations...................................................................................................49
Procedures and Functions as Parameters ......................................................................50
Graphics ......................................................................................................................50
Graphics Standards ........................................................................................51
Text and Graphics ..........................................................................................51
Bit Mapped Fonts ...........................................................................................51
Stroked Fonts .................................................................................................51
Graphics in Turbo PASCAL...........................................................................52
The Co-ordinate System .................................................................................52
Starting Graphics ...........................................................................................





Sample of the PDF document 









Programming in PASCAL


Programs and Data

Computers work on information in a manner roughly similar to the way sausage machines work on
sausage meat; something is fed in one end and, after processing, something else comes out the other
end. In the case of the sausage machine what it does with the sausage meat is always the same, in the
case of the computer different things happen to what goes in depending on what you tell the computer
to do with it.


This does not mean that everything the computer does with the information is correct, if you give the
computer the wrong instructions it will still try and do something and maybe produce some output, in
the same way that if you got the design of your sausage machine wrong you might still get something
out of the end. Neither does it mean that a computer will refuse to do something with incorrect
information, in the same way that a sausage machine would try to make sausages out of a bicycle if one
was placed in the feed hopper!

When we talk about data, we are talking about the information which the computer is working on. All
computer systems take in data in some way and then produce data as output. A program is the
sequence of instructions which you give the computer telling it what to do with the data. To change
what happens to the data you just have to change the program.

Because computers are machines which operate essentially on numbers a computer program usually
contains a lot of numeric manipulation. However the art of telling the computer what to do does not
require a great deal of numeric ability, any more than you need to known how to strip down a gearbox
in order to drive a Ford Mondeo.

A program is obeyed one step at the time by a computer system, starting at the first instruction and
working on until the end of the program is reached. Some constructions enable the direction of the
execution of the program to change, depending on the data which is being processed.



PASCAL and Programs
PASCAL was developed as a teaching language by Professor Niklaus Wirth at the Eidgenossische
Technische Hochschule in Zurich. It has however become popular as a mainstream programming
language, and is now available on most computer systems. In common with other programming
languages, its grammar and syntax are rigidly defined and a PASCAL compiler will reject any
program not adhering to the standard.

PASCAL is a structured language, in that it allows you to build up a program as a series of separate
parts which fitted together to construct a solution to the problem in hand. This leads to a programming
approach in which you divide the problem to be solved into a number of sub-tasks and then write a
series of smaller "programs" to solve each of them. You may find at this point that each subprogram
can be broken down into a number of even smaller subprograms and so on.
This form of programming methodology (!) is called Top Down and leads to a more manageable way
of solving problems, for example once the overall structure of the complete system has been decided
upon different people could work on each of the subprograms. It also means that you can build up a
"library" of useful subprograms which can be incorporated into later programs.
PASCAL also allows you to build structures to hold the data you are processing with your programs.
Early programming languages only provided mechanisms for storing very simple forms of data, of
mainly numeric form. PASCAL allows you to build up and manipulate a structure which holds items
of data specific to your application, of which more later.
A PASCAL program is broken up into several segments, broadly speaking first you define the form of
the data you are going to use and then you write the program which works on it: for example:
program simple (input, output);
var
i : integer ;
begin
Readln (i) ;
i := i * 2 ;
Writeln (i) ;
end .
This is a complete PASCAL program. There are no prizes for guessing what it does! Note that some of
the words are printed in bold. These are reserved words, i.e. they are used by PASCAL to mean
something. When you type your program you do not need to mark these words as anything special as
they will be recognised automatically. They have been printed in bold in the above for clarity only.
PASCAL recognises particular reserved words in certain positions in the program, according to the
syntax of the language. You have to be careful when writing your program that you do not use reserved
words in the wrong context, for example consider the implications of a program with the name begin!






Download Introduction to PASCAL / PDF







Introduction to PASCAL / PDF







0 commentaires: