CHPTR 1 Pascal
CHPTR 1 Pascal
1. Machine language -- actual binary code that gives basic instructions to the computer's
CPU. These are usually very simple commands like adding two numbers or moving data
from one memory location to another.
2. Assembly language -- a way for humans to program computers directly without
memorizing strings of binary numbers. There is a one-to-one correspondance with
machine code. For example, in Intel x86 machine language, ADD and MOV are mnemonics
for the addition and move operations.
3. High-level language -- permits humans to write complex programs without going step-
by step. High-level languages include Pascal, C, C++, FORTRAN, Java, BASIC, and
many more. One command in a high-level language, like writing a string to a file, may
translate to dozens or even hundreds of machine language instructions.
Microprocessors can only run machine language programs directly. Assembly language
programs are assembled, or translated into machine language. Likewise, programs written in
high-level languages, like Pascal, must also be translated into machine language before they can
be run. To do this translation is to compile a program.
The program that accomplishes the translation is called a compiler. This program is rather
complex since it not only creates machine language instructions from lines of code, but often
also optimizes the code to run faster, adds error-correction code, and links the code with
subroutines stored elsewhere. For example, when you tell the computer to print something to the
screen, the compiler translates this as a call to a pre-written module. Your code must then be
linked to the code that the compiler manufacturer provides before an executable program results.
With high-level languages, there are again three basic terms to remember:
1. Source code -- the code that you write. This typically has an extension that indicates the
language used. For example, Pascal source code usually ends in ".pas" and C++ code
usually ends in ".cpp"
2. Object code -- the result of compiling. Object code usually includes only one module of
a program, and cannot be run yet since it is incomplete. On DOS/Windows systems, this
usually has an extension of ".obj"
3. Executable code -- the end result. All the object code modules necessary for a program
to function are linked together. On DOS/Windows systems, this usually has an extension
of ".exe"
This product was a great success and was prominent for almost a decade. But in the 1990s, the
world was moving to Windows. In 1993, the last version of Turbo Pascal, version 7 for DOS,
came out. After that, the demand for DOS programs plummetted and Borland (renamed Inprise,
then back to Borland) focused on producing Windows compilers.
This tutorial will only deal with console-based programming, where the computer prints lines of
data to the screen and the user interacts with the program using a keyboard. The goal of the
tutorial is to teach how to program in Pascal. Once you've learned that, you can easily look at a
reference book or another web page and pick up graphics and windowing systems on your own.
Although old commercial Pascal compilers are often available for download, Turbo Pascal 5.5
from the Borland Museum and Symantec Think Pascal (Macintosh) linked from The Free
Country's Free Pascal Compiler List, computers have progressed much since the 1980s and early
1990s. We are no longer stuck with 8.3 filenames on DOS or non-preemptive multitasking on
Mac OS. Using an old compiler is fun in the same sense as playing an old game on an emulator,
but the open source movement has produced good compilers for modern operating systems, and
a beginner will find it much easier to use those.
GNU Pascal
Free Pascal
Free Pascal is generally considered friendlier for novices, and strives to emulate Borland Pascal
in many ways, though both will serve fine for learning Pascal. The easiest way for a novice to get
started with Pascal programming is to download the Lazarus IDE, which provides a friendly,
GUI-based environment for the Free Pascal compiler.
Here's how to get started, step by step, on Windows. Lazarus and Free Pascal are available on
other operating systems too; some steps will differ slightly.
1. Download Lazarus. If you're not sure whether you're running 32-bit or 64-bit, select 32-
bit since it is compatible with both.
2. Run through setup, accepting all the defaults.
3. Open Lazarus from the Lazarus folder in the Start menu.
4. Select the File-New... menu item, then select Program under the Project subtree.
5. Lazarus will create a source file with a bunch of stuff filled in. This is more complicated
than you need so far, so select all the content and delete.
6. Type in a program (flip to the next lesson to get a "Hello, world." program).
7. Save the file with File-Save As..., somewhere where you can find it easily later on. For
example, I saved the file as hello in a directory on my desktop. Lazarus will create two
files: hello.lpr which contains what I've typed in; and the project file, hello.lpi. Later, you
can double-click on either file to open the program in Lazarus.
8. Run the program from the Run menu. This will automatically compile the program if
you've made any changes, then run the program. It will also run the program without
compiling if you've not made any changes since the last time you compiled.
With programs that don't expect user input, you'll see a window flash up and disappear. This is
because the program finishes running in the blink of an eye, and closes itself. You are returned to
the IDE without seeing the results of your work. There are two ways around this:
Add a readln statement at the end of every program. Now the program will wait for you
to press the Enter key before it closes. If you're not familiar with the Command Prompt,
this is probably the easiest way to get what you want.
Note that a .exe file was created in the directory where you saved your program. This is
the executable. You can go to the Command Prompt, change to the directory, and run this
executable straight. You can also double-click on it in Windows Explorer (and it will still
flash by quickly if it ends without requiring user input).
We'll be learning only the fundamentals of Pascal in this tutorial, so we'll be sticking to console
programs that print text to the screen and interact with the keyboard. However, Lazarus and
Pascal are capable of producing programs as complicated as any that you have on your computer
right now. A web browser? A spreadsheet? A game? All possible, with thousands (or millions!)
of man-hours of effort.
--------------------------------------------
In the short history of computer programming, one enduring tradition is that the first program in
a new language is a "Hello, world" to the screen. So let's do that. Copy and paste the program
below into your IDE or text editor, then compile and run it.
If you have no idea how to do this, return to the Table of Contents. Earlier lessons explain what a
compiler is, give links to downloadable compilers, and walk you through the installation of an
open-source Pascal compiler on Windows.
program Hello;
begin
writeln ('Hello, world.')
end.
Hello, world.
If you're running the program in an IDE, you may see the program run in a flash, then return to
the IDE before you can see what happened. See the bottom of the previous lesson for the reason
why. One suggested solution, adding a readln to wait for you to press Enter before ending the
program, would alter the "Hello, world" program to become:
program Hello;
begin
writeln ('Hello, world.');
readln
end.
------------------------------------------------
CONST
(* Constant declarations *)
TYPE
(* Type declarations *)
VAR
(* Variable declarations *)
(* Subprogram definitions *)
BEGIN
(* Executable statements *)
END.
The elements of a program must be in the correct order, though some may be omitted if not
needed. Here's a program that does nothing, but has all the required elements:
program DoNothing;
begin
end.
Comments are portions of the code which do not compile or execute. Pascal comments start with
a (* and end with a *). You cannot nest comments:
(* (* *) *)
will yield an error because the compiler matches the first (* with the first *), ignoring the second
(* which is between the first set of comment markers. The second *) is left without its matching
(*. This problem with begin-end comment markers is one reason why many languages use line-
based commenting systems.
Turbo Pascal and most other modern compilers support brace comments, such as {Comment}.
The opening brace signifies the beginning of a block of comments, and the ending brace signifies
the end of a block of comments. Brace comments are also used for compiler directives.
Commenting makes your code easier to understand. If you write your code without comments,
you may come back to it weeks, months, or years later without a guide to why you coded the
program that way. In particular, you may want to document the major design of your program
and insert comments in your code when you deviate from that design for a good reason.
In addition, comments are often used to take problematic code out of action without deleting it.
Remember the earlier restriction on nesting comments? It just so happens that braces {} take
precedence over parentheses-stars (* *). You will not get an error if you do this:
{ (* Comment *) }
Whitespace (spaces, tabs, and end-of-lines) are ignored by the Pascal compiler unless they are
inside a literal string. However, to make your program readable by human beings, you should
indent your statements and put separate statements on separate lines. Indentation is often an
expression of individuality by programmers, but collaborative projects usually select one
common style to allow everyone to work from the same page.
Identifiers are names that allow you to reference stored values, such as variables and constants.
Also, every program and unit must be named by an identifier.
Different implementations of Pascal differ in their rules on special characters. Note that the
underscore character (_) is usually allowed.
Several identifiers are reserved in Pascal as syntactical elements. You are not allowed to use
these for your identifiers. These include but are not limited to:
and array begin case const div do downto else end file for forward function goto if in label mod
nil not of or packed procedure program record repeat set then to type until var while with
Modern Pascal compilers ship with much functionality in the API (Application Programming
Interfaces). For example, there may be one unit for handling graphics (e.g. drawing lines) and
another for mathematics. Unlike newer languages such as C# and Java, Pascal does not provide a
classification system for identifiers in the form of namespaces. So each unit that you use may
define some identifiers (say DrawLine) which you can no longer use. Pascal includes a system
unit which is automatically used by all programs. This provides baseline functionality such as
rounding to integer and calculating logarithms. The system unit varies among compilers, so
check your documentation. Here is the system unit documentation for Free Pascal Compiler.
Pascal is not case sensitive! (It was created in the days when all-uppercase computers were
common.) MyProgram, MYPROGRAM, and mYpRoGrAm are equivalent. But for readability purposes,
it is a good idea to use meaningful capitalization. Most programmers will be on the safe side by
never using two capitalizations of the same identifiers for different purposes, regardless of
whether or not the language they're using is case-sensitive. This reduces confusion and increases
productivity.
Identifiers can be any length, but some Pascal compilers will only look at the first several
characters. One usually does not push the rules with extremely long identifiers or loads of special
characters, since it makes the program harder to type for the programmer. Also, since most
programmers work with many different languages, each with different rules about special
characters and case-sensitivity, it is usually best to stick with alphanumeric characters and the
underscore character.
--------------------------------
Constants are referenced by identifiers, and can be assigned one value at the beginning of the
program. The value stored in a constant cannot be changed.
const
Identifier1 = value;
Identifier2 = value;
Identifier3 = value;
For example, let's define some constants of various data types: strings, characters, integers, reals,
and Booleans. These data types will be further explained in the next section.
const
Name = 'Tao Yue';
FirstLetter = 'a';
Year = 1997;
pi = 3.1415926535897932;
UsingNCSAMosaic = TRUE;
Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')! This contrasts
with newer languages which often use or allow double quotes or Heredoc notation. Standard
Pascal does not use or allow double quotes to mark characters or strings.
Constants are useful for defining a value which is used throughout your program but may change
in the future. Instead of changing every instance of the value, you can change just the constant
definition.
const
a : real = 12;
would yield an identifier a which contains a real value 12.0 instead of the integer value 12.
Variables are similar to constants, but their values can be changed as the program runs. Variables
must first be declared in Pascal before they can be used:
var
IdentifierList1 : DataType1;
IdentifierList2 : DataType2;
IdentifierList3 : DataType3;
...
IdentifierList is a series of identifiers, separated by commas (,). All identifiers in the list are
declared as being of the same data type.
integer
real
char
Boolean
Standard Pascal does not make provision for the string data type, but most modern compilers
do. Experienced Pascal programmers also use pointers for dynamic memory allocation, objects
for object-oriented programming, and many others, but this gets you started.
The integer data type can contain integers from -32768 to 32767. This is the signed
range that can be stored in a 16-bit word, and is a legacy of the era when 16-bit CPUs
were common. For backward compatibility purposes, a 32-bit signed integer is a longint
and can hold a much greater range of values.
The real data type has a range from 3.4x10-38 to 3.4x1038, in addition to the same range
on the negative side. Real values are stored inside the computer similarly to scientific
notation, with a mantissa and exponent, with some complications. In Pascal, you can
express real values in your code in either fixed-point notation or in scientific notation,
with the character E separating the mantissa from the exponent. Thus,
452.13 is the same as 4.5213e2
The char data type holds characters. Be sure to enclose them in single quotes, like so:
'a' 'B' '+' Standard Pascal uses 8-bit characters, not 16-bits, so Unicode, which is
used to represent all the world's language sets in one UNIfied CODE system, is not
supported.
The Boolean data type can have only two values:
TRUE and FALSE
var
age, year, grade : integer;
circumference : real;
LetterGrade : char;
DidYouFail : Boolean;
Once you have declared a variable, you can store values in it. This is called assignment.
variable_name := expression;
Note that unlike other languages, whose assignment operator is just an equals sign, Pascal uses a
colon followed by an equals sign, similarly to how it's done in most computer algebra systems.
some_real := 385.385837;
div and mod only work on integers. / works on both reals and integers but will always yield a
real answer. The other operations work on both reals and integers. When mixing integers and
reals, the result will always be a real since data loss would result otherwise. This is why Pascal
uses two different operations for division and integer division. 7 / 2 = 3.5 (real), but 7 div 2 =
3 (and 7 mod 2 = 1 since that's the remainder).
Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign
a real value to an integer variable. However, certain data types will convert to a higher data type.
This is most often done when assigning integer values to real variables. Suppose you had this
variable declaration section:
var
some_int : integer;
some_real : real;
some_int := 375;
some_real := some_int;
Changing one data type to another is referred to as typecasting. Modern Pascal compilers support
explicit typecasting in the manner of C, with a slightly different syntax. However, typecasting is
usually used in low-level situations and in connection with object-oriented programming, and a
beginning programming student will not need to use it. Here is information on typecasting from
the GNU Pascal manual.
In Pascal, the minus sign can be used to make a value negative. The plus sign can also be used to
make a value positive, but is typically left out since values default to positive.
This may make perfect sense to you, since you're trying to multiply by negative-2. However,
Pascal will be confused — it won't know whether to multiply or subtract. You can avoid this by
using parentheses to clarify:
The computer follows an order of operations similar to the one that you follow when you do
arithmetic. Multiplication and division (* / div mod) come before addition and subtraction (+
-), and parentheses always take precedence. So, for example, the value of: 3.5*(2+3) will be
17.5.
Pascal cannot perform standard arithmetic operations on Booleans. There is a special set of
Boolean operations. Also, you should not perform arithmetic operations on characters.
Pascal has several standard mathematical functions that you can utilize. For example, to find the
value of sin of pi radians:
Note that the sin function operates on angular measure stated in radians, as do all the
trigonometric functions. If everything goes well, value should become 0.
Functions are called by using the function name followed by the argument(s) in parentheses.
Standard Pascal functions include:
For ordinal data types (integer or char), where the allowable values have a distinct predecessor
and successor, you can use these functions:
Real is not an ordinal data type! That's because it has no distinct successor or predecessor. What
is the successor of 56.0? Is it 56.1, 56.01, 56.001, 56.0001?
However, for an integer 56, there is a distinct predecessor — 55 — and a distinct successor —
57.
'b'
Successor: 'c'
Predecessor: 'a'
The above is not an exhaustive list, as modern Pascal compilers include thousands of functions
for all sorts of purposes. Check your compiler documentation for more.
Since Pascal ignores end-of-lines and spaces, punctuation is needed to tell the compiler when a
statement ends.
The last statement in a BEGIN-END block, the one immediately preceding the END, does not
require a semicolon. However, it's harmless to add one, and it saves you from having to add a
semicolon if suddenly you had to move the statement higher up.
Indenting is not required. However, it is of great use for the programmer, since it helps to make
the program clearer. If you wanted to, you could have a program look like this:
program NotAsStupid;
const
a = 5;
b = 385.3;
var
alpha,
beta : real;
begin (* main *)
alpha := a + b;
beta := b / a
end. (* main *)
In general, indent each block. Skip a line between blocks (such as between the const and var
blocks). Modern programming environments (IDE, or Integrated Development Environment)
understand Pascal syntax and will often indent for you as you type. You can customize the
indentation to your liking (display a tab as three spaces or four?).
Proper indentation makes it much easier to determine how code works, but is vastly aided by
judicious commenting.
Now you know how to use variables and change their value. Ready for your first programming
assignment?
But there's one small problem: you haven't yet learned how to display data to the screen! How
are you going to know whether or not the program works if all that information is still stored in
memory and not displayed on the screen?
So, to get you started, here's a snippet from the next few lessons. To display data, use:
writeln (argument_list);
The argument list is composed of either strings or variable names separated by commas. An
example is:
Find the sum and average of five integers. The sum should be an integer, and the average should
be real. The five numbers are: 45, 7, 68, 2, and 34.
Use a constant to signify the number of integers handled by the program, i.e. define a constant as
having the value 5.
Then print it all out! The output should look something like this:
Number of integers = 5
Number1 = 45
Number2 = 7
Number3 = 68
Number4 = 2
Number5 = 34
Sum = 156
Average = 3.1200000000E+01
As you can see, the default output method for real numbers is scientific notation. Chapter 2 will
explain you how to format it to fixed-point decimal.
Here's one way to solve the programming assignment in the previous section.
(* Author: Tao Yue
Date: 19 June 1997
Description:
Find the sum and average of five predefined numbers
Version:
1.0 - original version
*)
program SumAverage;
const
NumberOfIntegers = 5;
var
A, B, C, D, E : integer;
Sum : integer;
Average : real;
begin (* Main *)
A := 45;
B := 7;
C := 68;
D := 2;
E := 34;
Sum := A + B + C + D + E;
Average := Sum / NumberOfIntegers;
writeln ('Number of integers = ', NumberOfIntegers);
writeln ('Number1 = ', A);
writeln ('Number2 = ', B);
writeln ('Number3 = ', C);
writeln ('Number4 = ', D);
writeln ('Number5 = ', E);
writeln ('Sum = ', Sum);
writeln ('Average = ', Average)
end. (* Main *)