TL07.TXT 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 28
  2. TURBO-LESSON 7: REPEAT STATEMENT
  3. OBJECTIVES - In lesson 7 you should learn about:
  4. 1. CHARacter variables
  5. 2. BOOLEAN variables
  6. 3. REPEAT statement
  7. 1. CHARacter variables.
  8. The reserved word, CHAR, is used to declare a variable of
  9. character type. A variable of type CHAR can be used to refer to
  10. or store a single character.
  11. VAR
  12. Alpha : CHAR;
  13. Alpha is declared to be a variable which can be used to store any
  14. of the characters in the character set. This includes the upper
  15. and lower case alphabet, the digits, 0 to 9, special characters
  16. such as #, $, %, *, and the rest of the 256 characters in the
  17. PC's character set.
  18. ##### DO:
  19. Examine PROG7.
  20. A variable named Response, of type CHAR, is used to store the
  21. character entered in response to a multiple choice question.
  22. ##### DO:
  23. Run the program several times, entering wrong responses, and the
  24. correct response, D. Also try lower case d.
  25. ##### DO:
  26. Study the first IF statement in PROG7.
  27. IF (Response = 'D') OR (Response = 'd')
  28. THEN . . .
  29. The character 'D' must be enclosed in single quotes in the
  30. program. Note that you enter the character as input data without
  31. quotes when running the program.
  32. Notice the compound condition using OR to combine two simple
  33. conditions. This condition will be true if either or both of
  34. the simple conditions are true. The correct response, D, is
  35. checked in both upper and lower case to make responding easier.
  36. ##### DO:
  37. Modify the IF statement to accept A, B, or C as the right response.
  38. Assume that the correct answer is A or B or C. (Ignore lower case
  39. responses to keep the statement short.)
  40. Œ
  41. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 29
  42. 2. BOOLEAN variables.
  43. BOOLEAN variables can have only two values, TRUE or FALSE. The
  44. value of a condition, TRUE or FALSE, may be stored in a BOOLEAN
  45. variable for later use.
  46. VAR
  47. Correct_Response : Boolean;
  48. In PROG7, the Boolean variable, Correct_Response, is used to
  49. store the truth value (TRUE or FALSE) of the condition in the
  50. first IF statement. If the correct character, 'D' or 'd', is
  51. entered, TRUE is stored in Correct_Response. If anything else is
  52. entered, FALSE is stored.
  53. Actually, TRUE and FALSE are stored as 1 and 0 to take up less
  54. space, but you can always view a BOOLEAN variable as having a
  55. value of TRUE or FALSE.
  56. ##### DO:
  57. Identify the condition in the second IF statement in PROG7.
  58. Since BOOLEAN variables can only have two values, TRUE or FALSE,
  59. and conditions always evaluate to the same two values, a BOOLEAN
  60. variable may be substituted for a condition.
  61. If would be permissible, but unnecessary to write the IF
  62. statement:
  63. IF Correct_Response = TRUE
  64. THEN . . .
  65. ##### DO:
  66. Modify the IF statement as indicated above and run the program to
  67. verify that the IF still works exactly as before.
  68. There is another way to assign the correct value to the variable,
  69. Correct_Response.
  70. ##### DO:
  71. Replace the FIRST IF statement in PROG7 with the following
  72. statement:
  73. Correct_Response := (Response = 'D') OR (Response = 'd');
  74. Run the program.
  75. How does the program change when you run it? (If it doesn't do
  76. exactly as before, maybe you typed it wrong, or replaced the
  77. second IF instead of the first?)
  78. Since the condition on the right of the := must be evaluated by
  79. the computer and assigned a value of TRUE or FALSE, this value
  80. can be stored directly in a BOOLEAN variable without using the IF
  81. statement.
  82. Œ
  83. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 30
  84. 3. REPEAT Statement.
  85. In a previous lesson, you learned that there are three ways to
  86. sequence the execution of statements in PASCAL: SIMPLE SEQUENCE,
  87. SELECTION STRUCTURES, and REPETITION STRUCTURES.
  88. One of the statements used for REPETITION is REPEAT . . UNTIL.
  89. The form of the REPEAT statement is:
  90. REPEAT
  91. Statement 1;
  92. Statement 2;
  93. .
  94. .
  95. .
  96. Statement n
  97. UNTIL condition;
  98. Statements 1, 2, . . . , n will be executed repeatedly until the
  99. condition becomes true. This implies that the condition is
  100. checking something that can be changed by the statements 1 to n.
  101. If this is not so, the statements will be repeated forever!
  102. In PROG7, you are prompted to respond to the multiple choice
  103. question. A REPEAT statement controls the block of statements
  104. which prompt for a response, and then check the response. The
  105. block of statements will be repeated until the UNTIL condition is
  106. true.
  107. ##### DO:
  108. Change the condition in the UNTIL in PROG7 to:
  109. UNTIL 'A' = 'B';
  110. Run the program.
  111. Does the program correctly identify a correct response? What
  112. happens then.
  113. (Use ctrl-c or ctrl-Scroll-Lock to stop the program.)
  114. ##### DO:
  115. Change the condition again to:
  116. UNTIL 'A' = 'A';
  117. How many times are you prompted for a response?
  118. Notice that the statements in a REPEAT structure are ALWAYS
  119. executed at least once. Even if the UNTIL condition is TRUE
  120. before entering the REPEAT, the condition is not checked until
  121. the end of the statements in the REPEAT block.
  122. Œ
  123. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 31
  124. ##### DO:
  125. Try to find another way to terminate the REPEAT without using the
  126. BOOLEAN variable, Correct_Response, in the UNTIL condition.
  127. Hint: Check the statement which assigns a value of TRUE or FALSE
  128. to Correct_Response.
  129. Œ