TL10.TXT 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 38
  2. TURBO-LESSON 10: WHILE STATEMENT
  3. OBJECTIVES - In lesson 10 you will learn about:
  4. 1. CONSTant declaration
  5. 2. WHILE statement
  6. 3. Delay timing loop
  7. 1. CONSTant declaration.
  8. In the declaration section of the Pascal programs in previous
  9. lessons, only VAR and PROGRAM have appeared.
  10. CONST is used to declare constants and assign values to them.
  11. The form is:
  12. CONST Constant_Name_1 = Value_1;
  13. Constant_Name_2 = Value_2;
  14. .
  15. .
  16. .
  17. Constant_Name_n = Value_n;
  18. Some examples:
  19. CONST PI = 3.14159;
  20. Fahr_Freeze = 32;
  21. Cels_Freeze = 0;
  22. Message_1 = 'This is a string constant';
  23. ##### DO:
  24. Run PROG10 a time or two to see what it does.
  25. ##### DO:
  26. Edit the constant declaration, Wobble_Size = 5;
  27. to Wobble_Size = 10;
  28. Run the program. What effect did you see from the change?
  29. Œ
  30. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 39
  31. ##### DO:
  32. Change the value of Left_Edge to 20 and run the program.
  33. To understand how Left_Edge and Wobble_Size work, look at the
  34. WriteLn statement:
  35. WriteLn(': ':Left_Edge, First:Index, Last:2, Message);
  36. (1) ': ' is printed in the right 2 columns of a field whose
  37. width is determined by the value of Left_Edge.
  38. (2) First initial is printed in the rightmost column of a field
  39. of width, Index. Note that Index is changing in the FOR
  40. loop, so the width of this field changes.
  41. (3) Last initial is printed next in the rightmost column of a
  42. 2-character wide field.
  43. (4) The string constant, Message, is printed immediately to the
  44. right of Last initial.
  45. ##### DO:
  46. Run the program several times with the values of the constants
  47. reset to various values. (LIVE DANGEROUSLY - try some wild
  48. values: 50 for Wobble_Size or 70 for Left_Edge. Your CRT won't
  49. explode! At least mine hasn't - yet!)
  50. 2. WHILE statement.
  51. The three Pascal REPETITION Structures are:
  52. (1) WHILE
  53. (2) REPEAT
  54. (3) FOR
  55. The form of the WHILE statement is:
  56. WHILE condition DO statement;
  57. Note that the statement is often a block statement and appears:
  58. WHILE condition DO
  59. BEGIN
  60. Statement_1;
  61. Statement_2;
  62. .
  63. .
  64. .
  65. Statement_n;
  66. END;
  67. The WHILE is executed "while" the condition is true.
  68. Contrast the REPEAT which is executed while the condition is
  69. false, UNTIL the condition becomes true.
  70. Œ
  71. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 40
  72. ##### DO:
  73. Run PROG10 again, using 0 as the number "less than 8".
  74. How many times was the loop executed?
  75. You could substitute REPEAT for WHILE in PROG10.
  76. ##### DO:
  77. Replace: WHILE Count > 0 DO
  78. with: REPEAT
  79. add: UNTIL Count = 0; after: END; {WHILE}
  80. (Notice that you don't have to remove the BEGIN END when you
  81. change the loop from WHILE to REPEAT. They are not needed in
  82. the REPEAT statement, but a block statement is always acceptable
  83. wherever a simple statement can be used.)
  84. Run the program several times to verify that it still works the
  85. same as with the WHILE loop.
  86. ##### DO:
  87. Run the program with 0 for the "number less than 8".
  88. How many times was the loop executed?
  89. Notice that the WHILE loop may be executed 0 times, but the
  90. REPEAT loop is always executed at least once, since the condition
  91. is not checked until the end of the loop.
  92. NOTE: YOU SHOULD BE ON THE LOOKOUT FOR SUCH CONTRASTS BETWEEN
  93. "INTERCHANGEABLE" STATEMENTS. CHOOSING THE BEST STATEMENT
  94. TO USE IN EACH CASE IS BASED ON THIS KIND OF AWARENESS.
  95. ##### DO:
  96. Change the REPEAT loop back to the original WHILE loop.
  97. (An easy way would be to load a fresh copy of the original
  98. PROG10.)
  99. Change the WHILE statement to accept only Last initials less than
  100. 'G'.
  101. WHILE Last < 'G' DO
  102. Run the program. How did it work?
  103. The WHILE loop didn't terminate at all - you had to abort the
  104. program, right? (If it is still executing, use Ctrl-C or Ctrl-
  105. Scroll-Lock.)
  106. Œ
  107. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 41
  108. One of the things needed in controlling loops: something in the
  109. loop must be related to the condition.
  110. In the original WHILE loop, the Count was being decreased in the
  111. loop.
  112. When the condition was changed to (Last < 'G'), the loop no
  113. longer has any effect on the condition.
  114. Either the loop will not be executed at all, if Last is = or >
  115. 'G', or it will execute indefinitely if Last is < 'G'.
  116. ##### DO:
  117. Change the WHILE statement to:
  118. WHILE (Last < 'G') AND (Count > 0) DO
  119. Run the program several times using last initials before and
  120. after G in the alphabet.
  121. ##### DO:
  122. Run the program with last initial of 'a' (lower-case a).
  123. How many times did the loop execute?
  124. Why?
  125. The ASCII value of 'G' is 71. The ASCII value of 'a' is 91. So
  126. 'a' is > 'G'. (More on how to handle this problem in a later
  127. lesson.)
  128. 3. Delay timing loop.
  129. You may have noticed the "wobble" proceeds faster in one
  130. direction than the other. This is due to a timing delay in the
  131. first WHILE loop.
  132. FOR Delay := 1 to 500 DO; {Do nothing, except loop}
  133. This looks a little strange? There should be a statement after
  134. the DO?
  135. Pascal has a "null" statement for just such times as this. When
  136. a statement is required, but none is present, Pascal just
  137. substitutes its "null" or "do-nothing" statement.
  138. Œ
  139. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 42
  140. ##### DO:
  141. Change the delay in the loop from 500 to 2000 and run the
  142. program.
  143. ##### DO:
  144. Add a timing loop to the other WHILE loop and run the program.
  145. How did it work?
  146. You didn't forget to add BEGIN END when you added the delay
  147. statement, did you?
  148. One last little twist on timing loops:
  149. ##### DO:
  150. Change the Wobble_Size to 10.
  151. Change both delay statements to:
  152. FOR Delay := 1 to 500 * Index DO;
  153. Run the program. Note that delays don't have to be constant.
  154. This delay varies as the FOR loop progresses!
  155. NOTE: I HOPE YOU HAVE ENJOYED THESE FIRST 10 TURBO-LESSONS. YOUR
  156. COMMENTS AND SUGGESTIONS WOULD BE WELCOME!
  157.