home *** CD-ROM | disk | FTP | other *** search
-
-
-
- Chapter 2
- GETTING STARTED IN PASCAL
-
-
- YOUR FIRST PASCAL PROGRAM
- ____________________________________________________________
-
- Lets get right into a program that really =================
- does nothing, but is an example of the TRIVIAL.PAS
- most trivial Pascal program. Load Turbo =================
- Pascal, select TRIVIAL.PAS as a Work file,
- and select Edit. This assumes that you
- have been successful in learning how to use the TURBO Pascal
- system. If you are using TURBO Pascal 4.0 through 5.5, you
- will need to load TRIVIAL.PAS from the File menu.
-
- You should now have the most trivial Pascal program possible
- on your display, and we can take a look at each part to define
- what it does.
-
- The first line is required in the standard Pascal definition
- and is the program name which can be any name you like, as
- long as it follows the rules for an identifier given in the
- next paragraph. It can have no blanks, otherwise it would be
- considered as two words and it would confuse the compiler.
- The first word program is the first of the reserved words
- mentioned earlier and it is the indicator to the Pascal
- compiler that this is the name of the program. Notice that
- the line ends with a semicolon. Pascal uses the semicolon as
- a statement separator and although all statements do not
- actually end in a semicolon, most do, and use of the semicolon
- will clear up later in your mind.
-
- TURBO Pascal does not require the program statement, but to
- remain compatible with standard Pascal, it will simply ignore
- the entire statement. It is recommended that you include a
- program name both to aid your thinking in standard Pascal, and
- to add a little more indication of the purpose of each
- program.
-
-
- WHAT IS AN IDENTIFIER?
- ____________________________________________________________
-
- All identifiers, including the program name, procedure and
- function names, type definitions, and constant and variable
- names, will start with an alphabetical character and be
- composed of any combination of alphabetic and numeric
- characters with no embedded blanks. Upper or lower case
- alphabetic characters are not significant and may be mixed at
- will. (If you find this definition confusing at this point,
- don't worry about it, it will be clear later but it must be
- defined early). The standard definition of Pascal requires
- that any implementation (i.e. any compiler written by some
-
- Page 2-1
-
- Getting Started in Pascal
-
- company) must use at least 8 characters of the identifier as
- significant and may ignore the remaining characters if more
- than 8 are used. Most implementations use far more than 8.
- TURBO Pascal uses at least 63 characters in an identifier as
- being significant.
-
- Standard Pascal does not allow the use of underlines in an
- identifier but most implementations of Pascal allow its use
- after the first character. All versions of TURBO Pascal
- compilers permit the use of the underline in an identifier,
- so it will be freely used throughout this tutorial. The
- underline is used in the program name Puppy_Dog which should
- be on your display at this time.
-
- Returning to the example program, the next line is a blank
- line which is ignored by all Pascal compilers. More will be
- said about the blank line at the end of this chapter.
-
-
- NOW FOR THE PROGRAM
- ____________________________________________________________
-
- Lines 3 and 4 comprise the actual Pascal program, which in
- this case does absolutely nothing. It is an illustration of
- the minimum Pascal program. The two words begin and end are
- the next two reserved words we will consider. Any logical
- grouping of Pascal code can be isolated by bracketing it with
- the two reserved words begin and end. You will use this
- construct repeatedly as you write Pascal code so it is well
- to learn it thoroughly. Code to be executed by conditional
- jumps will be bracketed by begin and end, as will code within
- a loop, and code contained within a subroutine (although they
- are called procedures in Pascal), and in many other ways. In
- the present program, the begin and end are used to bracket the
- main program and every Pascal program will have the main
- program bracketed in this manner. Because there is nothing
- to do in this program, there are no statements between the
- begin and end reserved words.
-
- Finally, although it could be very easily overlooked, there
- is one more very important part of the program, the period
- following the reserved word end. The period is the signal to
- the compiler that it has reached the end of the executable
- statements and is therefore finished compiling. Every Pascal
- program will have one, and only one period in it and that one
- period will be at the end of the program. I must qualify that
- statement in this regard, a period can be used in comments,
- and in text to be output. In fact there are some data formats
- that require using a period as part of their structure. The
- statement is true however, that there is only one period in
- the executable part of a Pascal program. Think of a Pascal
- program as one long sentence with one period at the end.
- Ignore lines 9 through 13 for a few minutes and we will
- describe them fully later.
-
- Page 2-2
-
- Getting Started in Pascal
-
-
- That should pretty well describe our first program. Now it
- is time to compile and run it after you exit the editor. The
- means of doing this is given for each compiler as an aid to
- get you started.
-
- TURBO Pascal 3.0 - <ctrl>k, r
-
- TURBO Pascal 4.0 - <alt>r
-
- TURBO Pascal 5.x - <alt>r (then <alt>F5 to view the
- results)
-
- Since this program doesn't do anything, it is not very
- interesting, so let's get one that does something.
-
-
- A PROGRAM THAT DOES SOMETHING
- ____________________________________________________________
-
- Load the Pascal program WRITESM.PAS and =================
- view it on your monitor. The filename is WRITESM.PAS
- sort of cryptic for "Write Some" and it =================
- will display a little output on the
- monitor. The program name is Kitty_Cat
- which says nothing about the program itself but can be any
- identifier we choose. We still have the begin and end to
- define the main program area followed by the period. However,
- now we have two additional statements between the begin and
- end. Writeln is a special word and it is probably not
- surprising that it means to write a line of data somewhere.
- Without a modifier, which will be fully explained in due time,
- it will write to the default device which, in the case of our
- IBM compatible, is the video display. The data within the
- parentheses is the data to be output to the display and
- although there are many kinds of data we may wish to display,
- we will restrict ourselves to the simplest for the time being.
- Any information between apostrophes will simply be output as
- text information.
- The special word Writeln is not a reserved word but is defined
- by the system to do a very special job for you, namely to
- output a line of data to the monitor. It is, in fact, a
- procedure supplied for you by the writers of TURBO Pascal as
- a programming aid for you. You can, if you so desire, use
- this name for some other purpose in your program, but doing
- so will not allow you to use the standard output procedure.
- It will then be up to you to somehow get your data out of the
- program.
- Note carefully that some words are reserved and cannot be
- redefined and used for some other purpose, and some are
- special since they can be redefined. You will probably not
- want to redefine any of the special words for a long time so
- simply use them as tools.
-
-
- Page 2-3
-
- Getting Started in Pascal
-
- Notice the semicolon at the end of line 4. This is the
- statement separator referred to earlier and tells Pascal that
- this line is complete as it stands, nothing more is coming
- that could be considered part of this statement. The next
- statement, in line 5, is another statement that will be
- executed sequentially following the statement in line 4. This
- program will output the two lines of text and stop. Now it
- is time to go try it. Compile and run the program in the same
- manner as you did for the first example program.
-
- You should see the two lines of text output to the video
- display every time you run this program. When you grow bored
- of running WRITESM.PAS let's go on to another example.
-
-
-
- ANOTHER PROGRAM WITH MORE OUTPUT
- ____________________________________________________________
-
- Examine the example program named =================
- WRITEMR.PAS. This new program has three WRITEMR.PAS
- lines of output but the first two are =================
- different because another special word is
- introduced to us, namely Write. Write is
- a procedure which causes the text to be output in exactly the
- same manner as Writeln, but Write does not cause a carriage
- return to be output. Writeln causes its output to take place
- then returns the "carriage" to the first character of the next
- line. The end result is that all three of the lines of text
- will be output on the same line of the monitor when the
- program is run. Notice that there is a blank at the end of
- each of the first two lines so that the formatting will look
- nice. Compile and execute the new program.
-
- Now might be a good time for you to return to editing
- WRITEMR.PAS and add a few more output commands to see if they
- do what you think they should do. When you tire of that, we
- will go on to the next file and learn about comments within
- a Pascal program.
-
-
-
- ADDING COMMENTS IN THE PROGRAM
- ____________________________________________________________
-
- The file named PASCOMS.PAS is similar to =================
- the others except that comments have been PASCOMS.PAS
- added to illustrate their use. Pascal =================
- defines comments as anything between (*
- and *) or anything between { and }.
- Originally only the wiggly brackets were defined, but since
- many keyboards didn't have them available, the parenthesis
- star combination was defined as an extension and is universal
- by now, so you can use either. Most of the comments are self
-
- Page 2-4
-
- Getting Started in Pascal
-
- explanatory except for the one within the code. Since
- comments can go from line to line, the two lines that would
- print "send money" are not Pascal code but are commented out.
- Try compiling and running this program, then edit the comments
- out so that "send money" is printed also.
-
- A fine point should be mentioned here. Even though some
- compilers allow comments to start with (* and end with }, or
- to start with { and end with *), it is very poor programming
- practice and should be discouraged. The ANSI Pascal standard
- allows such usage but TURBO Pascal does not allow this funny
- use of comment delimiters.
-
- TURBO Pascal does not allow you to nest comments using the
- same delimiters but it does allow you to nest one type within
- the other. This could be used as a debugging aid. If you
- generally use the (* and *) for comments, you could use the
- { and } in TURBO Pascal to comment out an entire section of
- code during debugging even if it had a few comments in it.
- This is a trick you should remember when you reach the point
- of writing programs of significant size.
-
- When you have successfully modified and run the program with
- comments, we will go on to explain good formatting practice
- and how Pascal actually searches through your source file
- (Pascal program) for its executable statements.
-
- It should be mentioned that the program named PASCOMS.PAS does
- not indicate good commenting style. The program is meant to
- illustrate where and how comments can be used and looks very
- choppy and unorganized. Further examples will illustrate good
- use of comments to you as you progress through this tutorial.
-
-
-
- THE RESULT OF EXECUTION SECTION
- ____________________________________________________________
-
- You should now be able to discern the purpose for lines 20
- through 26 of this program. Each of the example programs in
- this tutorial lists the result of execution in a similar
- comments section at the end of the program. This makes it
- possible to study this tutorial anywhere once you print out
- the example programs as described in the READ.ME file on the
- distribution disk. With this text, and a hard copy of the
- example programs containing the result of execution, you do
- not need access to a computer to study. Of course you would
- need access to a computer to write, compile, and execute the
- programming exercises, which you are heartily encouraged to
- do.
-
-
-
-
-
- Page 2-5
-
- Getting Started in Pascal
-
- GOOD FORMATTING PRACTICE
- ____________________________________________________________
-
- Examine GOODFORM.PAS to see an example of ================
- good formatting style. It is important to GOODFORM.PAS
- note that Pascal doesn't give a hoot where ================
- you put carriage returns or how many
- blanks you put in when a blank is called
- for as a delimiter. Pascal only uses the combination of
- reserved words and end-of- statement semicolons to determine
- the logical structure of the program. Since we have really
- only covered two executable statements, I have used them to
- build a nice looking program that can be easily understood at
- a glance. Compile and run this program to see that it really
- does what you think it should do.
-
-
-
- VERY POOR FORMATTING PRACTICE
- ____________________________________________________________
-
- Examine UGLYFORM.PAS now to see an example ================
- of terrible formatting style. It is not UGLYFORM.PAS
- really apparent at a glance but the ================
- program you are looking at is exactly the
- same program as the last one. Pascal
- doesn't care which one you ask it to run because to Pascal,
- they are identical. To you they are considerably different,
- and the second one would be a mess to try to modify or
- maintain sometime in the future.
-
- UGLYFORM.PAS should be a good indication to you that Pascal
- doesn't care about programming style or form. Pascal only
- cares about the structure, including reserved words and
- delimiters such as blanks and semicolons. Carriage returns
- are completely ignored as are extra blanks. You can put extra
- blanks nearly anywhere except within reserved words or
- variable names. You should pay some attention to programming
- style but don't get too worried about it yet. It would be
- good for you to simply use the style illustrated throughout
- this tutorial until you gain experience with Pascal. As time
- goes by you will develop a style of statement indentation,
- adding blank lines for clarity, and a method of adding clear
- comments to Pascal source code. Programs are available to
- read your source code, and put it in a "pretty" format, but
- that is not important now.
-
- Not only is the form of the program important, the names used
- for variables can be very helpful or hindering as we will see
- in the next chapter. Feel free to move things around and
- modify the format of any of the programs we have covered so
- far and when you are ready, we will start on variables in the
- next chapter. Be sure you compile and execute UGLYFORM.PAS.
-
-
- Page 2-6
-
- Getting Started in Pascal
-
-
- PROGRAMMING EXERCISES
- ____________________________________________________________
-
- 1. Write a program that displays your name on the video
- monitor.
-
- 2. Modify your program to display your name and address on
- one line, then modify it by changing the Write's to
- Writeln's so that the name and address are on different
- lines.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Page 2-7