home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / PASTUT24.ZIP / PTUTRTXT.ZIP / CHAP02.TXT < prev    next >
Encoding:
Text File  |  1989-12-01  |  16.2 KB  |  414 lines

  1.  
  2.  
  3.  
  4.                                                     Chapter 2
  5.                                     GETTING STARTED IN PASCAL
  6.  
  7.  
  8. YOUR FIRST PASCAL PROGRAM
  9. ____________________________________________________________
  10.  
  11. Lets get right into a program that really   =================
  12. does nothing, but is an example of the         TRIVIAL.PAS
  13. most trivial Pascal program. Load Turbo     =================
  14. Pascal, select TRIVIAL.PAS as a Work file,
  15. and select Edit.  This assumes that you
  16. have been successful in learning how to use the TURBO Pascal
  17. system.  If you are using TURBO Pascal 4.0 through 5.5, you
  18. will need to load TRIVIAL.PAS from the File menu.
  19.  
  20. You should now have the most trivial Pascal program possible
  21. on your display, and we can take a look at each part to define
  22. what it does.
  23.  
  24. The first line is required in the standard Pascal definition
  25. and is the program name which can be any name you like, as
  26. long as it follows the rules for an identifier given in the
  27. next paragraph.  It can have no blanks, otherwise it would be
  28. considered as two words and it would confuse the compiler. 
  29. The first word program is the first of the reserved words
  30. mentioned earlier and it is the indicator to the Pascal
  31. compiler that this is the name of the program.  Notice that
  32. the line ends with a semicolon.  Pascal uses the semicolon as
  33. a statement separator and although all statements do not
  34. actually end in a semicolon, most do, and use of the semicolon
  35. will clear up later in your mind.  
  36.  
  37. TURBO Pascal does not require the program statement, but to
  38. remain compatible with standard Pascal, it will simply ignore
  39. the entire statement.  It is recommended that you include a
  40. program name both to aid your thinking in standard Pascal, and
  41. to add a little more indication of the purpose of each
  42. program.
  43.  
  44.  
  45. WHAT IS AN IDENTIFIER?
  46. ____________________________________________________________
  47.  
  48. All identifiers, including the program name, procedure and
  49. function names, type definitions, and constant and variable
  50. names, will start with an alphabetical character and be
  51. composed of any combination of alphabetic and numeric
  52. characters with no embedded blanks.  Upper or lower case
  53. alphabetic characters are not significant and may be mixed at
  54. will.  (If you find this definition confusing at this point,
  55. don't worry about it, it will be clear later but it must be
  56. defined early).  The standard definition of Pascal requires
  57. that any implementation (i.e. any compiler written by some
  58.  
  59.                                                      Page 2-1
  60.  
  61.                                     Getting Started in Pascal
  62.  
  63. company) must use at least 8 characters of the identifier as
  64. significant and may ignore the remaining characters if more
  65. than 8 are used.  Most implementations use far more than 8. 
  66. TURBO Pascal uses at least 63 characters in an identifier as
  67. being significant.
  68.  
  69. Standard Pascal does not allow the use of underlines in an
  70. identifier but most implementations of Pascal allow its use
  71. after the first character.  All versions of TURBO Pascal
  72. compilers permit the use of the underline in an identifier,
  73. so it will be freely used throughout this tutorial.  The
  74. underline is used in the program name Puppy_Dog which should
  75. be on your display at this time.
  76.  
  77. Returning to the example program, the next line is a blank
  78. line which is ignored by all Pascal compilers.  More will be
  79. said about the blank line at the end of this chapter.
  80.  
  81.  
  82. NOW FOR THE PROGRAM
  83. ____________________________________________________________
  84.  
  85. Lines 3 and 4 comprise the actual Pascal program, which in
  86. this case does absolutely nothing.  It is an illustration of
  87. the minimum Pascal program.  The two words begin and end are
  88. the next two reserved words we will consider.  Any logical
  89. grouping of Pascal code can be isolated by bracketing it with
  90. the two reserved words begin and end.  You will use this
  91. construct repeatedly as you write Pascal code so it is well
  92. to learn it thoroughly.  Code to be executed by conditional
  93. jumps will be bracketed by begin and end, as will code within
  94. a loop, and code contained within a subroutine (although they
  95. are called procedures in Pascal), and in many other ways.  In
  96. the present program, the begin and end are used to bracket the
  97. main program and every Pascal program will have the main
  98. program bracketed in this manner.  Because there is nothing
  99. to do in this program, there are no statements between the
  100. begin and end reserved words.
  101.  
  102. Finally, although it could be very easily overlooked, there
  103. is one more very important part of the program, the period
  104. following the reserved word end.  The period is the signal to
  105. the compiler that it has reached the end of the executable
  106. statements and is therefore finished compiling.  Every Pascal
  107. program will have one, and only one period in it and that one
  108. period will be at the end of the program.  I must qualify that
  109. statement in this regard, a period can be used in comments,
  110. and in text to be output.  In fact there are some data formats
  111. that require using a period as part of their structure.  The
  112. statement is true however, that there is only one period in
  113. the executable part of a Pascal program.  Think of a Pascal
  114. program as one long sentence with one period at the end. 
  115. Ignore lines 9 through 13 for a few minutes and we will
  116. describe them fully later.
  117.  
  118.                                                      Page 2-2
  119.  
  120.                                     Getting Started in Pascal
  121.  
  122.  
  123. That should pretty well describe our first program.  Now it
  124. is time to compile and run it after you exit the editor.  The
  125. means of doing this is given for each compiler as an aid to
  126. get you started.
  127.  
  128.   TURBO Pascal 3.0 - <ctrl>k, r
  129.  
  130.   TURBO Pascal 4.0 - <alt>r
  131.  
  132.   TURBO Pascal 5.x - <alt>r  (then <alt>F5 to view the
  133. results)
  134.  
  135. Since this program doesn't do anything, it is not very
  136. interesting, so let's get one that does something.
  137.  
  138.  
  139. A PROGRAM THAT DOES SOMETHING
  140. ____________________________________________________________
  141.  
  142. Load the Pascal program WRITESM.PAS and     =================
  143. view it on your monitor.  The filename is      WRITESM.PAS
  144. sort of cryptic for "Write Some" and it     =================
  145. will display a little output on the
  146. monitor.  The program name is Kitty_Cat
  147. which says nothing about the program itself but can be any
  148. identifier we choose.  We still have the begin and end to
  149. define the main program area followed by the period.  However,
  150. now we have two additional statements between the begin and
  151. end.  Writeln is a special word and it is probably not
  152. surprising that it means to write a line of data somewhere. 
  153. Without a modifier, which will be fully explained in due time,
  154. it will write to the default device which, in the case of our
  155. IBM compatible, is the video display.  The data within the
  156. parentheses is the data to be output to the display and
  157. although there are many kinds of data we may wish to display,
  158. we will restrict ourselves to the simplest for the time being. 
  159. Any information between apostrophes will simply be output as
  160. text information.
  161. The special word Writeln is not a reserved word but is defined
  162. by the system to do a very special job for you, namely to
  163. output a line of data to the monitor.  It is, in fact, a
  164. procedure supplied for you by the writers of TURBO Pascal as
  165. a programming aid for you.  You can, if you so desire, use
  166. this name for some other purpose in your program, but doing
  167. so will not allow you to use the standard output procedure. 
  168. It will then be up to you to somehow get your data out of the
  169. program.  
  170. Note carefully that some words are reserved and cannot be
  171. redefined and used for some other purpose, and some are
  172. special since they can be redefined.  You will probably not
  173. want to redefine any of the special words for a long time so
  174. simply use them as tools.  
  175.  
  176.  
  177.                                                      Page 2-3
  178.  
  179.                                     Getting Started in Pascal
  180.  
  181. Notice the semicolon at the end of line 4.  This is the
  182. statement separator referred to earlier and tells Pascal that
  183. this line is complete as it stands, nothing more is coming
  184. that could be considered part of this statement.  The next
  185. statement, in line 5, is another statement that will be
  186. executed sequentially following the statement in line 4.  This
  187. program will output the two lines of text and stop.  Now it
  188. is time to go try it.  Compile and run the program in the same
  189. manner as you did for the first example program.
  190.  
  191. You should see the two lines of text output to the video
  192. display every time you run this program.  When you grow bored
  193. of running WRITESM.PAS let's go on to another example.
  194.  
  195.  
  196.  
  197. ANOTHER PROGRAM WITH MORE OUTPUT
  198. ____________________________________________________________
  199.  
  200. Examine the example program named           =================
  201. WRITEMR.PAS.  This new program has three       WRITEMR.PAS
  202. lines of output but the first two are       =================
  203. different because another special word is
  204. introduced to us, namely Write.  Write is
  205. a procedure which causes the text to be output in exactly the
  206. same manner as Writeln, but Write does not cause a carriage
  207. return to be output.  Writeln causes its output to take place
  208. then returns the "carriage" to the first character of the next
  209. line.  The end result is that all three of the lines of text
  210. will be output on the same line of the monitor when the
  211. program is run.  Notice that there is a blank at the end of
  212. each of the first two lines so that the formatting will look
  213. nice.  Compile and execute the new program.
  214.  
  215. Now might be a good time for you to return to editing
  216. WRITEMR.PAS and add a few more output commands to see if they
  217. do what you think they should do.  When you tire of that, we
  218. will go on to the next file and learn about comments within
  219. a Pascal program.
  220.  
  221.  
  222.  
  223. ADDING COMMENTS IN THE PROGRAM
  224. ____________________________________________________________
  225.  
  226. The file named PASCOMS.PAS is similar to    =================
  227. the others except that comments have been      PASCOMS.PAS
  228. added to illustrate their use.  Pascal      =================
  229. defines comments as anything between (*
  230. and *) or anything between { and }. 
  231. Originally only the wiggly brackets were defined, but since
  232. many keyboards didn't have them available, the parenthesis
  233. star combination was defined as an extension and is universal
  234. by now, so you can use either.  Most of the comments are self
  235.  
  236.                                                      Page 2-4
  237.  
  238.                                     Getting Started in Pascal
  239.  
  240. explanatory except for the one within the code.  Since
  241. comments can go from line to line, the two lines that would
  242. print "send money" are not Pascal code but are commented out. 
  243. Try compiling and running this program, then edit the comments
  244. out so that "send money" is printed also.
  245.  
  246. A fine point should be mentioned here.  Even though some
  247. compilers allow comments to start with (* and end with }, or
  248. to start with { and end with *), it is very poor programming
  249. practice and should be discouraged.  The ANSI Pascal standard
  250. allows such usage but TURBO Pascal does not allow this funny
  251. use of comment delimiters.
  252.  
  253. TURBO Pascal does not allow you to nest comments using the
  254. same delimiters but it does allow you to nest one type within
  255. the other.  This could be used as a debugging aid.  If you
  256. generally use the (* and *) for comments, you could use the
  257. { and } in TURBO Pascal to comment out an entire section of
  258. code during debugging even if it had a few comments in it. 
  259. This is a trick you should remember when you reach the point
  260. of writing programs of significant size.
  261.  
  262. When you have successfully modified and run the program with
  263. comments, we will go on to explain good formatting practice
  264. and how Pascal actually searches through your source file
  265. (Pascal program) for its executable statements.
  266.  
  267. It should be mentioned that the program named PASCOMS.PAS does
  268. not indicate good commenting style.  The program is meant to
  269. illustrate where and how comments can be used and looks very
  270. choppy and unorganized.  Further examples will illustrate good
  271. use of comments to you as you progress through this tutorial.
  272.  
  273.  
  274.  
  275. THE RESULT OF EXECUTION SECTION
  276. ____________________________________________________________
  277.  
  278. You should now be able to discern the purpose for lines 20
  279. through 26 of this program.  Each of the example programs in
  280. this tutorial lists the result of execution in a similar
  281. comments section at the end of the program.  This makes it
  282. possible to study this tutorial anywhere once you print out
  283. the example programs as described in the READ.ME file on the
  284. distribution disk.  With this text, and a hard copy of the
  285. example programs containing the result of execution, you do
  286. not need access to a computer to study.  Of course you would
  287. need access to a computer to write, compile, and execute the
  288. programming exercises, which you are heartily encouraged to
  289. do.
  290.  
  291.  
  292.  
  293.  
  294.  
  295.                                                      Page 2-5
  296.  
  297.                                     Getting Started in Pascal
  298.  
  299. GOOD FORMATTING PRACTICE
  300. ____________________________________________________________
  301.  
  302. Examine GOODFORM.PAS to see an example of    ================
  303. good formatting style.  It is important to     GOODFORM.PAS
  304. note that Pascal doesn't give a hoot where   ================
  305. you put carriage returns or how many
  306. blanks you put in when a blank is called
  307. for as a delimiter.  Pascal only uses the combination of
  308. reserved words and end-of- statement semicolons to determine
  309. the logical structure of the program.  Since we have really
  310. only covered two executable statements, I have used them to
  311. build a nice looking program that can be easily understood at
  312. a glance.  Compile and run this program to see that it really
  313. does what you think it should do. 
  314.  
  315.  
  316.  
  317. VERY POOR FORMATTING PRACTICE
  318. ____________________________________________________________
  319.  
  320. Examine UGLYFORM.PAS now to see an example   ================
  321. of terrible formatting style.  It is not       UGLYFORM.PAS
  322. really apparent at a glance but the          ================
  323. program you are looking at is exactly the
  324. same program as the last one.  Pascal
  325. doesn't care which one you ask it to run because to Pascal,
  326. they are identical.  To you they are considerably different,
  327. and the second one would be a mess to try to modify or
  328. maintain sometime in the future.
  329.  
  330. UGLYFORM.PAS should be a good indication to you that Pascal
  331. doesn't care about programming style or form.  Pascal only
  332. cares about the structure, including reserved words and
  333. delimiters such as blanks and semicolons.  Carriage returns
  334. are completely ignored as are extra blanks.  You can put extra
  335. blanks nearly anywhere except within reserved words or
  336. variable names.  You should pay some attention to programming
  337. style but don't get too worried about it yet.  It would be
  338. good for you to simply use the style illustrated throughout
  339. this tutorial until you gain experience with Pascal.  As time
  340. goes by you will develop a style of statement indentation,
  341. adding blank lines for clarity, and a method of adding clear
  342. comments to Pascal source code.  Programs are available to
  343. read your source code, and put it in a "pretty" format, but
  344. that is not important now.
  345.  
  346. Not only is the form of the program important, the names used
  347. for variables can be very helpful or hindering as we will see
  348. in the next chapter.  Feel free to move things around and
  349. modify the format of any of the programs we have covered so
  350. far and when you are ready, we will start on variables in the
  351. next chapter.  Be sure you compile and execute UGLYFORM.PAS.
  352.  
  353.  
  354.                                                      Page 2-6
  355.  
  356.                                     Getting Started in Pascal
  357.  
  358.  
  359. PROGRAMMING EXERCISES
  360. ____________________________________________________________
  361.  
  362. 1.   Write a program that displays your name on the video
  363.      monitor. 
  364.  
  365. 2.   Modify your program to display your name and address on
  366.      one line, then modify it by changing the Write's to
  367.      Writeln's so that the name and address are on different
  368.      lines.
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.                                                      Page 2-7
  414.