lan

English French German Spain Italian Dutch Russian Portuguese Japanese Korean Arabic Chinese Simplified
click the next picture To continue

Friday 22 October 2010

Learn Pascal in Three Days

Your First Pascal Program
The Pascal program may be as simple as the one in Example 1-1. It displays on your
screen the phrase “Hi there.”
{ ----------------------------- Example 1-1 ------------------------------ }
PROGRAM FirstProgram(OUTPUT);
BEGIN
WRITELN('Hi there')
END.
Whether the Pascal program is small or large, it must have a specific structure. This
program consists mainly of one statement (WRITELN) which does the actual work
here, as it displays whatever comes between the parentheses. The statement is
included inside a frame starting with the keyword BEGIN and ending with the keyword
END. This is called the program main body (or the program block) and usually
contains the main logic of data processing.
Comments
Consider the first line in the program:
{ ----------------------------- Example 1-1 ------------------------------ }
This is a comment and is totally ignored by the compiler. Comments can appear anywhere
in the Pascal program between two braces ({}) or between the two symbols
(* and *) thus:
(* This is a comment *)
1
Program Heading
The second line is called the program heading. It starts with the keyword PROGRAM
followed by a space, followed by the program name (FirstProgram). The program
name is a user-invented word. User-invented words are classified in Pascal as identifiers.
An identifier must begin with a letter and may contain any number of letters or
digits (in Turbo Pascal it may contain underscores as well). You are free to choose any
meaningful name for your program, but do not expect a program name like “BEGIN”
or “PROGRAM” to be accepted. These words are called reserved words, and they are
only used in the proper place in the program. Pascal reserved words are summarized
in Appendix B.
The program name is followed by the word OUTPUT contained in parentheses and
terminated with a semicolon:
PROGRAM FirstProgram(OUTPUT);
The keyword OUTPUT tells the compiler that this program is going to produce output
(such as writing to the screen), which is the counterpart of INPUT (such as reading
from the keyboard). The words OUTPUT and INPUT are called file parameters. The
program may perform both input and output, in which case the file parameters take
the form:
PROGRAM FirstProgram(INPUT,OUTPUT);
In Turbo Pascal the program heading is optional. You may skip the whole line and start
your program with the word BEGIN, or you may use the program name without
parameters, like this:
PROGRAM FirstProgram;
Syntax and Conventions
The most important syntax is the semicolon after the program heading (which is used
as a separator) and the period after the word END (which terminates the program).
A common convention is to write Pascal keywords in uppercase and the user-invented
names (identifiers) in lowercase with the first letter capitalized. If the name consists of
more than one word (which is the case in this program), the first letter in each word is
capitalized. So, in Pascal programs you may see identifiers like:
Wages
PayRoll
HoursWorkedPerWeek
This is just a convention to make your program readable, but Pascal compilers are not
case sensitive. This means that you can write the entire program in lowercase as in
2 Chapter 1
􀀀􀀀
􀀀􀀀
Example 1-2, or in uppercase as in Example 1-3. All three of the programs will compile
and run.
{ ------------------------------ Example 1-2 ------------------------------ }
program firstprogram(output);
begin
writeln('Hi there')
end.
{ ------------------------------ Example 1-3 ------------------------------ }
PROGRAM FIRSTPROGRAM(OUTPUT);
BEGIN
WRITELN('Hi there')
END.
All blank lines, indentations, and spaces (except those following the Pascal keywords)
are optional, but it is a good programming habit to use this method to make your program
well-organized and readable।
1-2 Displaying Text: WRITELN, WRITE
To display several lines of text you need a WRITELN statement for each line, as in the
following program in Example 1-4. Be sure to put quotes around text strings.
NOTE A companion CD-ROM comes with this book to help you save time and
effort. This disc contains the source code of all examples, in addition to the
solutions of the drills. Please read the Readme.txt or Readme.htm file on the
distribution disc. It contains the instructions for installing the files on your hard drive.
Notice that the Readme.htm file starts up automatically when you insert the CD into
the drive.
{ ------------------------------ Example 1-4 ------------------------------ }
PROGRAM LinesOfText(OUTPUT);
BEGIN
WRITELN('Hi there.');
WRITELN('How are you today?');
WRITELN('Are you ready for Pascal?')
END.
Now the program contains more than one statement. Each statement must be separated
from the next one with a semicolon. This is the only way the compiler can
recognize the end of a statement, but for the last statement in the program block you
may skip the semicolon.
When you compile this program it will give the following output:
Hi there.
How are you today?
Hello Pascal 3
Are you ready for Pascal?
The WRITELN statement displays a line of text followed by a new line (a linefeed and
a carriage return). If you wish to display two strings on the same line, you need to use
the WRITE statement as shown in the following program.
{ ------------------------------ Example 1-5 ------------------------------ }
PROGRAM TwoLines(OUTPUT);
BEGIN
WRITE('Hi there. ');
WRITELN('How are you today?');
WRITELN('Are you ready for Pascal?')
END.
The output of this program is:
Hi there. How are you today?
Are you ready for Pascal?
As you can see in the program output, the second string is written on the same line as
the first string as a result of using the WRITE statement to display the first string.
This is the only difference between the two output statements WRITE and
WRITELN.
If you want to display a blank line, you only need the statement:
WRITELN;
Drill 1-1
Write a Pascal program to display the following text on the screen:
Wordware Publishing, Inc.
-------------------------
2320 Los Rios Boulevard
Plano, Texas 75074
-Crunching Numbers
The easiest task for any program is to crunch numbers. The statement WRITELN (or
WRITE) can be used both to display numbers and evaluate numerical expressions. You
can build up arithmetic expressions using the following arithmetic operators:
+ addition
– subtraction
* multiplication
/ division
4 Chapter 1
Take a look at these examples:
WRITELN(123);
WRITELN(1.23 * 4);
The first example displays the number between the parentheses (123). The second
example performs multiplication of two numbers and displays the result. Notice that
for numeric values, unlike text strings, you don’t use quotes.
You may use WRITELN to display text and numbers in the same statement by using
the comma as a separator like this:
WRITELN('The result is=', 125 * 1.75);
The following program is used to evaluate two numeric expressions (multiplication
and division) and display the results preceded by the proper text.
{ ------------------------------ Example 1-6 -------------------------------}
PROGRAM CrunchNumbers(OUTPUT);
BEGIN
WRITELN('I can easily crunch numbers.');
WRITELN('Here is multiplication of 50x4:',50*4);
WRITELN('..and here is division of 2400/8:',2400/8)
END.
The output of this program is:
I can easily crunch numbers.
Here is multiplication of 50x4:200
..and here is division of 2400/8: 3.0000000000E+02
The multiplication is done as expected. The two operands (50 and 4) were integers
(whole numbers) and the result (200) was an integer too. The division result, however,
came out in a format that needs some explanation.
Integers and Real Numbers
The division performed with the operator / is called real division and always produces
as its result a real number. Real numbers may be written in fixed-point notation (such
as 300.0) or in scientific (exponential) notation (such as 3.0E+02), but in Pascal, real
number output will always be represented in scientific notation by default. A number
written in scientific notation is made up of two parts divided by the letter E (or e). The
left part is called the mantissa and indicates the significant digits, while the right part
is called the exponent. The exponent is a power of ten that determines the position of
the decimal point. So, in this example the number:
3.0000000000E+02
is the same as the number:
3 x 102
Hello Pascal 5
The same number, when expressed in fixed-point format, becomes:
300.0
If the exponent is preceded by a minus sign as in:
3.124E–02
then the decimal point is shifted two positions to the left. This number, then, is the
same as:
0.03124
If the number is negative, the minus sign should precede the mantissa:
–0.0124E–02
If the number is positive, you may omit the sign for either the mantissa or the
exponent:
1.23E02
The division operator (/) is called the real division operator, because the result always
appears as a real number regardless of the type of the operands.
For integer division use the operator DIV as in the example:
WRITELN(2400 DIV 8);
This will produce the output 300.
With integer division, any fraction in the result will be truncated, as in this example:
WRITELN(9 DIV 4); produces the output 2
Another important operator, MOD, is used to get the remainder of integer division
(modulo), as in these examples:
WRITELN(9 MOD 4); produces the output 1
WRITELN(3 MOD 4); produces the output 3
The operators DIV and MOD take only integer operands and produce integer output.
For the other operators (+, –, and *), if either one of the operands is real, the result
will be real.
to download this course click here

No comments:

Post a Comment