To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97
Chapter 1 HELLO Pascal
1-1 Your First Pascal Program
The Pascal program may be as simple as the one in figure 1-1. It displays on your screen the phrase Hi there.
{ --------------------------- figure 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:
{ --------------------------- figure 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 *)
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 figure 1-2, or in uppercase as in figure 1-3. All three of the programs will compile and run.
{ --------------------------- figure 1-2 --------------------------- }
program firstprogram(output);
begin
writeln('Hi there')
end.
{ --------------------------- figure 1-3 --------------------------- }
PROGRAM FIRSTPROGRAM(OUTPUT);
BEGIN
WRITELN('Hi there')
END.
All blank lines, indentation, 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 figure 1-4.
NOTE: A companion diskette comes with this book to help you save time and effort. This diskette contains the source code of all examples, in addition to the solutions of the drills. Please read the readme file on the distribution disk. Just type the command README and press Enter.
{ --------------------------- figure 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?
Are you ready for Pascal?
The WRITELN statement displays a line of text followed by a new line (a line feed and a carriage return). If you wish to display two strings on the same line, you need to use the statement WRITE as shown in the following program.
{ --------------------------- figure 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 statement WRITE 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
|
|