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 most trivial Pascal program. Load
- Turbo Pascal, select TRIVIAL 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, 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 the 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 version 3.0 does not require the "program"
- statement, but to remain compatible with standard Pascal, it
- will simply ignore the entire statement. I like to include
- a program name both to keep me thinking in standard Pascal,
- and to add a little more indication of the purpose of each
- program.
-
- WHAT IS AN IDENTIFIER?
-
- All identifiers, including 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 company) must use at least 8 characters of the
- identifier as significant and may ignore the remaining
- characters if more are used. Most implementations use far
-
-
-
- Page 7
-
-
-
-
-
-
-
-
-
- CHAPTER 2 - Getting started in Pascal
-
-
- 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. Both TURBO Pascal compilers
- allow the use of the underline as an allowable character 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 that at the end of this chapter.
-
- NOW FOR THE PROGRAM
-
- The next two lines 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.
-
- Finally, although it could be very easily overlooked,
- there is one more very important part of the program, the
- period following "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.
-
- That should pretty well describe our first program.
- Now it is time to compile and run it. To do so you must
-
-
- Page 8
-
-
-
-
-
-
-
-
-
- CHAPTER 2 - Getting started in Pascal
-
-
- exit the editor using Ctrl-K-D, unless you modified the exit
- command.
-
- Compile the program, and run it to observe the result.
- 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 and view it on your
- monitor. The filename is sort of cryptic for "Write Some"
- and it will give a little output to 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, (to
- be 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
- possibilities of display information, we will restrict
- ourselves to the simplest for the time being. Any data
- 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.
-
- 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.
-
-
- Page 9
-
-
-
-
-
-
-
-
-
- CHAPTER 2 - Getting started in Pascal
-
-
- Now it is time to go try it. Exit the editor, then compile
- and run the program.
-
- You should get the two lines of text output to the
- video display every time you run it. When you grow bored of
- running WRITESM lets go on to another example.
-
- ANOTHER PROGRAM WITH MORE OUTPUT
-
- Load and edit WRITEMR. This new program has three
- lines of output but the first two are different because
- another special word is introduced to us, namely Write.
- Write causes the text to be output in exactly the same
- manner as Writeln, but Write does not cause a carriage
- return. 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 will be
- output on the same line 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. Exit the
- editor now and try the new program.
-
- Now might be a good time for you to return to editing
- WRITEMR 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 is similar to the others except
- that comments have been 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 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.
-
-
-
- Page 10
-
-
-
-
-
-
-
-
-
- CHAPTER 2 - Getting started in Pascal
-
-
- 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 PASCOMS 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.
-
- GOOD FORMATTING PRACTICE
-
- Observe GOODFORM to see an example of good formatting
- style. It is important to 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
-
- Edit UGLYFORM now to see an example of terrible
- formatting style. It is not 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 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
-
-
- Page 11
-
-
-
-
-
-
-
-
-
- CHAPTER 2 - Getting started in Pascal
-
-
- variable names. You should pay some attention to
- programming style but don't get too worried about it yet.
- 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 run UGLYFORM.
-
- 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 12
-