TL03.TXT 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 12
  2. TURBO-LESSON 3: PROGRAM STRUCTURE
  3. OBJECTIVES - In this lesson you will learn about:
  4. 1. Program structure
  5. 2. Compiler Directive, $U+
  6. 3. Write and WriteLn statements
  7. 4. Comments - at beginning of program
  8. 5. Comments - at end of statement line
  9. 6. Comments - kill a section of code temporarily
  10. 1. Program structure.
  11. PASCAL programs are written in a certain way to make them easier
  12. to write and easier to read. There are two parts to any PASCAL
  13. program: the DECLARATIONS part and the PROGRAM BODY.
  14. The PROGRAM BODY is where the processing statements occur, and
  15. any data item used there must first be defined in the
  16. DECLARATIONS section. The form of a PASCAL program is
  17. PROGRAM Demo; (Program name - not required)
  18. LABEL (Labels, if used, go here)
  19. CONST (Constants are defined here)
  20. TYPE (Define your own data types here)
  21. VAR (ALL variables must be defined here)
  22. BEGIN (Processing statements occur between
  23. END. BEGIN and END.)
  24. The simplest PASCAL program is:
  25. BEGIN
  26. END.
  27. ##### DO:
  28. Type in the program:
  29. BEGIN
  30. END.
  31. Exit the editor and type R to execute the program.
  32. What happened? (If you hold the R key down to run the program
  33. over and over, you may see the message, "running").
  34. Notice that no compile errors or run time errors occurred
  35. (evidenced by the absence of error messages on the screen).
  36. Œ
  37. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 13
  38. Definitely not the most useful program, but it illustrates a
  39. point: the MAIN BODY is always required and as much of the
  40. DECLARATIONS section as needed to define the data used. Since
  41. this simple program uses no data, the DECLARATIONS section can
  42. be omitted.
  43. Standard PASCAL requires that declarations occur in the order
  44. specified and each type of declaration may occur only once.
  45. TURBO is a little more forgiving in some respects than standard
  46. PASCAL (declarations don't have to occur in the specified order,
  47. and the same type of declaration may occur more than once).
  48. 2. Compiler Directive, $U+
  49. Compiler directives are used to control special features provided
  50. by the compiler. The directive, $U+, which occurs at the
  51. beginning of most of the sample programs, ensures that you will
  52. be able to stop a program without rebooting.
  53. 3. Write and WriteLn statements.
  54. These processing statements are introduced first, because they
  55. can be used with messages which require no previous data
  56. declaration. This allows you to experiment with some programming
  57. in the MAIN BODY without any concern for the DECLARATIONS part.
  58. ##### DO:
  59. Load PROG3 and use the editor to examine the program. A part of
  60. PROG3 is printed below:
  61. BEGIN
  62. WriteLn('* * * * * * * * * * * * * * * * * *'); {Note that comments }
  63. WriteLn('* *'); {placed at the end of a }
  64. WriteLn('* TURBO-LESSON 3 *'); {statement line should }
  65. WriteLn('* *'); {be terminated on the }
  66. WriteLn('* Edited, Compiled, Executed by *'); {same line. A multiline }
  67. WriteLn('* (put your name here) *'); {comment, like the one at}
  68. WriteLn('* *'); {the beginning of this }
  69. WriteLn('* * * * * * * * * * * * * * * * * *'); {program would include }
  70. { some of the non-comment statements, making them ineffective. }
  71. In this section of PROG3, the WriteLn statement is used to
  72. display messages.
  73. ##### DO:
  74. RUN the program to see the messages displayed on the screen.
  75. (Ctrl-K, Ctrl-D to exit editor, then R to run the program. Refer
  76. to lessons 1 and 2 and your reference manual for more help using
  77. TURBO and the editor.)
  78. Œ
  79. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 14
  80. ##### DO:
  81. Use the editor to insert your name in the line indicated and
  82. run the program again.
  83. To illustrate the difference between WriteLn and Write
  84. statements, another portion of the program is included below:
  85. WriteLn;
  86. Write ('Check carefully when ');
  87. Write ('you run this program. ');
  88. Write ('How many lines ');
  89. WriteLn('are printed');
  90. WriteLn('by this last set of Write and WriteLn statements?');
  91. The "Ln" in WriteLn may be viewed as a linefeed or "return the
  92. cursor to the beginning of the next line". Note that the
  93. instruction is WriteLn, not LnWrite. The order is significant.
  94. Any message, or other data item within the parentheses following
  95. WriteLn is printed first, the "Write" part, then the cursor is
  96. moved to the next line, the "Ln" part.
  97. Notice the first WriteLn in the program segment above. There is
  98. nothing in parentheses to print, so this statement is a way to
  99. print a blank line.
  100. The next three Write statements write on the same line. Then the
  101. following WriteLn statement writes it's message and causes the
  102. cursor to return to the beginning of the next line.
  103. The last WriteLn displays it's message on the line where the
  104. cursor is positioned, and returns the cursor to the next line.
  105. ##### DO:
  106. Add some WriteLn statements after the BEGIN to insert several
  107. blank lines at the top of the screen to push the boxed message
  108. down the screen.
  109. Run the program. Did it work right?
  110. 4. Comments - at the beginning of program.
  111. There are two ways to indicate comments in a PASCAL program. The
  112. curly brackets, { }, may be used to enclose comments. The
  113. original comment delimiters, from standard PASCAL are (* *).
  114. At the beginning of a program, or anywhere that multiple line
  115. comments are needed, it is convenient to mark the whole block of
  116. comments with the delimiters at the left margin. This makes it
  117. easy to insert or delete comments within the block.
  118. Œ
  119. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 15
  120. ##### DO:
  121. Add some comments at the beginning of the program. Try both types
  122. of comment delimiters, { }, and (* *).
  123. 5. Comments - at the end of a statement line.
  124. Look at the comments following the WriteLn statements in PROG3.
  125. Short comments may be inserted to clarify the action of one or
  126. more statements. NOTICE CAREFULLY that each of these comments
  127. has to be completed on the same line, if the next line is another
  128. active processing statement.
  129. ##### DO:
  130. Add a short comment, {Blank Line}, after one of the WriteLn statements which
  131. prints a blank line.
  132. 6. Comments - kill a section of code.
  133. Having two sets of comment delimiters makes it possible to use
  134. one set for most comments, while reserving the second set for
  135. "killing" sections of statements when debugging a program.
  136. I prefer to use the { } for most comments, and (* *) for killing
  137. code. You should keep the delimiters used for commenting out
  138. (killing) code in the left margin where they are very visible.
  139. ##### DO:
  140. Kill the three WriteLn statements following the one which prints
  141. TURBO-LESSON 3.
  142. Run the program to see the results.
  143. DON'T BE AFRAID TO EXPERIMENT WITH THE SAMPLE PROGRAMS. YOU CAN
  144. ALWAYS GET ANOTHER COPY, FROM YOUR BACKUP DISK (you did make one,
  145. didn't you), FROM THE .BAK FILE PROVIDED BY TURBO, OR FROM THE
  146. ORIGINAL FILE IF YOU HAVEN'T SAVED AN EDITED COPY.
  147. Œ