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

  1.  
  2.  
  3.  
  4.                                                     Chapter 4
  5.                                    LOOPS & CONTROL STRUCTURES
  6.  
  7.  
  8.  
  9. Every program we have examined to this point has been a simple
  10. one pass through with no statements being repeated.  As in all
  11. other languages, Pascal has extensive capabilities to do
  12. looping and conditional branching.  We will look at these now.
  13.  
  14.  
  15. THE FOR LOOP
  16. ____________________________________________________________
  17.  
  18. We will start with what may be the easiest   ================
  19. structure to understand, the for loop.         LOOPDEMO.PAS
  20. This is used to repeat a single Pascal       ================
  21. statement any number of times we desire. 
  22. Load LOOPDEMO.PAS and we will discuss the
  23. loops presented there. 
  24.  
  25. The example illustrated in lines 13 and 14, is the simplest
  26. loop and does nothing more than execute a Writeln 7 times. 
  27. We have three new reserved words, for, to, and do which are
  28. used as shown.  Any simple variable of type integer, byte, or
  29. char can be used for the loop index but due to the requirement
  30. that everything must be defined prior to use in Pascal, the
  31. loop index must be defined in a var statement.  Following the
  32. reserved word do is any single Pascal statement that will be
  33. repeated the specified number of times.  Note that the loop
  34. is an incrementing loop but substitution of downto for to will
  35. make it a decrementing loop as is illustrated in the last
  36. example in this program.  It should be pointed out that the
  37. loop control variable can only be incremented or decremented
  38. by 1 each time through the loop in Pascal.
  39.  
  40.  
  41. A COMPOUND PASCAL STATEMENT
  42. ____________________________________________________________
  43.  
  44. The example given in lines 18 through 22 contains our first
  45. compound Pascal statement.  It was mentioned in Chapter 1 that
  46. the begin end pair of reserved words could be used to mark the
  47. limits of a compound statement.  In this case, the single
  48. statement starting with the begin at the end of line 18 and
  49. extending through and including the end statement in line 22
  50. is the single Pascal statement that will be executed 10 times. 
  51. A second variable Total has been introduced to simply add
  52. another operation to the loop.  Any valid Pascal operation can
  53. be performed within the begin end pair, including another for
  54. loop, resulting in nested loops to whatever depth you desire. 
  55.  
  56. The third example shows how the char type variable could be
  57. used in a for loop.  Pascal requires that the loop index, the
  58.  
  59.                                                      Page 4-1
  60.  
  61.                                  Loops and Control Structures
  62.  
  63. starting point, and the ending point all be of the same type
  64. or it will generate an error message during compilation.  In
  65. addition, they must be variables of type integer, byte, or
  66. char.  The starting point and ending point can be constants
  67. or expressions of arbitrary complexity.
  68.  
  69. The fourth example is a decrementing loop as mentioned
  70. earlier.  It uses the reserved word downto, and should be self
  71. explanatory.
  72.  
  73.  
  74. THE IF STATEMENT
  75. ____________________________________________________________
  76.  
  77. Pascal has two conditional branching         ================
  78. capabilities, the if and the case               IFDEMO.PAS
  79. statements.  We will look at the simplest    ================
  80. of the two now, the if statement.  Load
  81. IFDEMO.PAS for an onscreen look at the if
  82. then pair of reserved words first illustrated in lines 11 and
  83. 12.  Any condition that can be reduced to a boolean answer is
  84. put between the if then pair of words.  If the resulting
  85. expression resolves to TRUE, then the single Pascal statement
  86. following the reserved word then is executed, and if it
  87. resolves to FALSE, then the single statement is skipped over. 
  88. Of course, you can probably guess that the single statement
  89. can be replaced with a compound statement bracketed with a
  90. begin end pair and you are correct.  Study example 1 and you
  91. will see that the line will always be printed in this
  92. particular fragment because Three is equal to One + Two.  It
  93. is very difficult to come up with a good example without
  94. combining some of the other control structures but we will do
  95. so in the next example program.
  96.  
  97. The second example in lines 14 through 19, is similar to the
  98. first but has the single statement replaced with a compound
  99. statement and should be simple for you to understand.
  100.  
  101. The third example in lines 21 through 24, contains a new
  102. reserved word, else.  When the if condition is FALSE, the
  103. single statement following the word then is skipped and if a
  104. semicolon is encountered, the if clause is totally complete. 
  105. If instead of a semicolon, the reserved word else is
  106. encountered, then the single Pascal statement following else
  107. is executed.  One and only one of the two statements will be
  108. executed every time this if statement is encountered in the
  109. program.  Examination of the third example should clear this
  110. up in your mind.
  111.  
  112. Notice that the Pascal compiler is looking for either a
  113. semicolon to end the if, or the reserved word else to continue
  114. the logic.  It is therefore not legal to use a semicolon
  115. immediately preceding the reserved word else.  You will get
  116. a compiler error if you include the semicolon.
  117.  
  118.                                                      Page 4-2
  119.  
  120.                                  Loops and Control Structures
  121.  
  122.  
  123. THE IF-THEN-ELSE BLOCK
  124. ____________________________________________________________
  125.  
  126. Put on your thinking cap because the next principle is
  127. difficult to grasp at first but will suddenly clear up and be
  128. one of the most useful facts of Pascal programming.  Since the
  129. entire if then else block of code is itself a single Pascal
  130. statement by definition, it can be used anywhere that an
  131. executable statement is legal without begin end separators. 
  132. This is shown in the fourth example of the IFDEMO.PAS Pascal
  133. example program.  Lines 27 through 30 comprise a single Pascal
  134. statement, and lines 32 through 35 comprise another.  The if
  135. statement begun in line 26 therefore has a single statement
  136. in each of its branches, and it is a single Pascal statement
  137. itself beginning in line 26 and ending in line 35.
  138.  
  139. The if then else construct is one of the most used, most
  140. useful, and therefore most important aspects of Pascal.  For
  141. this reason you should become very familiar with it.
  142.  
  143. Try changing some of the conditions in the example program to
  144. see if you can get it to print when you expect it to for your
  145. own practice.  When you are ready, we will go on to a program
  146. with loops and conditional statements combined and working
  147. together.
  148.  
  149.  
  150. LOOPS AND IFS TOGETHER
  151. ____________________________________________________________
  152.  
  153. Load LOOPIF.PAS and study it for a few       ================
  154. minutes.  It contains most of what you          LOOPIF.PAS
  155. have studied so far and should be            ================
  156. understandable to you at this point.  It
  157. contains a loop (lines 7 through 17) with
  158. two if statements within it (lines 8 & 9 and lines 10 through
  159. 16), and another loop (lines 11 through 15) within one of the
  160. if statements.
  161.  
  162. You should make careful note of the formatting used here.  The
  163. begin is at the end of the line which starts the control and
  164. the end is lined up under the control word such that it is
  165. very clear which control word it is associated with.  You will
  166. develop your own clear method of formatting your code in time
  167. but until then it is suggested that you follow this example
  168. which is written in a manner which is acceptable within the
  169. Pascal programming community.
  170.  
  171. An easily made error should be pointed out at this time.  If
  172. an extraneous semicolon were put at the end of the if
  173. statement in line 8, the code following the statement would
  174. always be executed because the null statement (the nothing
  175. statement between the then and the semicolon) would be the
  176.  
  177.                                                      Page 4-3
  178.  
  179.                                  Loops and Control Structures
  180.  
  181. conditional statement.  The compiler would not generate an
  182. error and you would get no warning.  Add a semicolon at the
  183. end of line 8 to see the error.  Of course, you will need to
  184. compile and execute the program to see line 9 print for all
  185. 10 values of Count.
  186.  
  187.  
  188. FINALLY, A MEANINGFUL PROGRAM
  189. ____________________________________________________________
  190.  
  191. Load TEMPCONV.PAS and study its structure.   ================
  192. Notice the header block that defines the       TEMPCONV.PAS
  193. program and gives a very brief explanation   ================
  194. of what the program does.  This program
  195. should pose no problem to you in
  196. understanding what it does since it is so clearly documented. 
  197. Run it and you will have a list of Centigrade to Fahrenheit
  198. temperature conversions with a few added notes.  
  199.  
  200. Load, examine, and run DUMBCONV.PAS for a    ================
  201. good example of poor variable naming.  The     DUMBCONV.PAS
  202. structure of the program is identical to     ================
  203. the last program and when you run it, you
  204. will see that it is identical in output,
  205. but compared to the last program, it is difficult to
  206. understand what it does by studying the listing.  We studied
  207. UGLYFORM.PAS in chapter 2 of this tutorial and it illustrated
  208. really stupid formatting that nobody would ever use.  The poor
  209. choice of variable names and lack of comments in the present
  210. program is nearly as unreadable, but many programmers are
  211. content to write code similar to this example.  You should be
  212. conscious of good formatting style and naming conventions from
  213. the start and your programs will be clear, easy to understand,
  214. and will run efficiently.  This program, like the last should
  215. be easily understood by you, so we will go on to our next
  216. Pascal control structure.
  217.  
  218.  
  219. THE REPEAT UNTIL LOOP
  220. ____________________________________________________________
  221.  
  222. The next two Pascal constructs are very similar because they
  223. are both indefinite loops (indefinite because they are not
  224. executed a fixed number of times).  One of the loops is
  225. evaluated at the top and the other at the bottom.  It will
  226. probably be easier to start with the repeat until construct
  227. which is the loop that is evaluated at the bottom.
  228.  
  229. Retrieve the file REPEATLP.PAS to see an     ================
  230. example of a repeat loop.  Two more            REPEATLP.PAS
  231. reserved words are defined here, namely      ================
  232. repeat and until.  This rather simple
  233. construct simply repeats all statements
  234. between the two reserved words until the boolean expression
  235.  
  236.                                                      Page 4-4
  237.  
  238.                                  Loops and Control Structures
  239.  
  240. following the until is found to be TRUE.  This is the only
  241. expression in Pascal that operates on a range of statements
  242. rather than a single statement and begin end delimiters are
  243. not required.  
  244.  
  245. A word of caution is in order here.  Since the loop is
  246. executed until some condition becomes TRUE, it is possible
  247. that the condition will never be TRUE and the loop will never
  248. terminate.  It is up to you, the programmer, to insure that
  249. the loop will eventually terminate.  Compile and execute
  250. REPEATLP.PAS to observe the output.
  251.  
  252.  
  253. THE WHILE LOOP
  254. ____________________________________________________________
  255.  
  256. The file WHILELP.PAS contains an example    =================
  257. of another new construct, the while loop.      WHILELP.PAS
  258. This uses the while do reserved words and   =================
  259. will execute one Pascal statement (or one
  260. compound statement bounded with begin and
  261. end) continuously until the boolean expression between the two
  262. words becomes FALSE.  
  263.  
  264. This loop is also indeterminate and could, like the repeat
  265. until loop, never terminate.  You should therefore exercise
  266. care in using it. 
  267.  
  268. There are two basic differences in the last two loops.  The
  269. repeat until loop is evaluated at the bottom of the loop and
  270. must therefore always go through the loop at least one time. 
  271. The while loop is evaluated at the top and may not go through
  272. even once.  This gives you flexibility when choosing the loop
  273. to do the job at hand.  Compile, run, and examine the output
  274. from the example program WHILELP.PAS.
  275.  
  276.  
  277. THE CASE STATEMENT
  278. ____________________________________________________________
  279.  
  280. The final control structure introduces one more reserved word,
  281. case.  The case construct actually should be included with the
  282. if statement since it is a conditional execution statement,
  283. but we saved it for last because it is rather unusual and will
  284. probably be used less than the others we have discussed in
  285. this chapter.
  286.  
  287. The case statement is used to select one     ================
  288. of many possible simple Pascal statements      CASEDEMO.PAS
  289. to execute based on the value of a simple    ================
  290. variable.  Load the file CASEDEMO.PAS and
  291. observe the program for an example of a
  292. case statement.  The variable between the case and of reserved
  293. words in line 9 is the variable used to make the selection and
  294.  
  295.                                                      Page 4-5
  296.  
  297.                                  Loops and Control Structures
  298.  
  299. is called the case selector.  Following that, the various
  300. selections are listed as a possible value or range, followed
  301. by a colon, a single Pascal statement, and a semicolon for
  302. each selector.  Following the list of selections, an else can
  303. be added to cover the possibility that none of the selections
  304. were executed.  Finally, an end statement is used to terminate
  305. the case construct.  Note that this is one of the few places
  306. in Pascal that an end is used without a corresponding begin.
  307.  
  308. The example file uses Count for the case selector, prints the
  309. numbers one through five in text form, and declares that
  310. numbers outside this range are not in the allowable list.  The
  311. program should be self explanatory beyond that point.  Be sure
  312. to compile and run this example program.
  313.  
  314. Load and display the sample program         =================
  315. BIGCASE.PAS for another example of a case      BIGCASE.PAS
  316. statement with a few more added features.   =================
  317. This program uses the identical structure
  318. as the previous program but in line 11 a
  319. range is used as the selector so that if the value of Count
  320. is 7, 8, or 9 this selection will be made.  In line 12, three
  321. different listed values will cause selection of this part of
  322. the code.  Of greater importance are the compound statements
  323. used in some of the selections.  If the variable Count has the
  324. value of 2, 4, or 6, a compound statement will be executed and
  325. if the value is 3, a for loop is executed.  If the value is
  326. 1, an if statement is executed which will cause a compound
  327. statement to be executed.  In this case the if statement will
  328. always be executed because TRUE will always be true, but any
  329. Boolean expression could be used in the expression.  Be sure
  330. to compile and run this program, then study the output until
  331. you understand the result thoroughly.
  332.  
  333. This brings us to the end of chapter 4 and you now have enough
  334. information to write essentially any program desired in
  335. Pascal.  You would find however, that you would have a few
  336. difficulties if you attempted to try to write a very big
  337. program without the topics coming up in the next few chapters. 
  338. The additional topics will greatly add to the flexibility of
  339. Pascal and will greatly ease programming with it.
  340.  
  341.  
  342. PROGRAMMING EXERCISES
  343. ____________________________________________________________
  344.  
  345. 1.   Write a program that lists the numbers from 1 to 12 and
  346.      writes a special message beside the number representing
  347.      your month of birth.
  348.  
  349. 2.   Write a program that lists all of the numbers from 1 to
  350.      12 except for the numbers 2 and 9.
  351.  
  352.  
  353.  
  354.                                                      Page 4-6
  355.