TL08.TXT 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 32
  2. TURBO-LESSON 8: CASE STATEMENT
  3. OBJECTIVES - In lesson 8 you will learn about:
  4. 1. Block statements
  5. 2. CASE statement
  6. 1. Block statement.
  7. As noted in an earlier lesson, the form of the IF statement is:
  8. IF condition THEN statement_1 ELSE statement_2;
  9. IF the condition is true, statement_1 is executed, otherwise
  10. statement_2 is executed. However, a single statement may not
  11. always get the job done.
  12. The Block statement (also called a Compound statement) allows you
  13. to substitute a multiple statement block anywhere a simple
  14. statement is acceptable. The form of the Block statement is:
  15. BEGIN Statement_1; Statement_2 END;
  16. You can include as many statements as you like between the BEGIN
  17. and END.
  18. Notice that the main body of a Pascal program is a single
  19. statement! That single statement is a block statement:
  20. BEGIN
  21. Statement_1;
  22. Statement_2;
  23. .
  24. .
  25. .
  26. Statement_n;
  27. END.
  28. To illustrate the use of a Block statement in an IF statement,
  29. consider the following problem:
  30. If the value of I is greater than the value of J, swap the two in
  31. memory (a common problem when sorting data).
  32. If you happen to have a single statement to swap the values of
  33. two memory locations, the IF statement might be:
  34. IF I > J
  35. THEN SWAP(I,J);
  36. Œ
  37. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 33
  38. Later, you will learn how to make up your own "statements" (by
  39. creating functions and procedures), but for now, the way to swap
  40. I and J is:
  41. IF I > J
  42. THEN
  43. BEGIN
  44. Temp := I;
  45. I := J;
  46. J := I;
  47. END;
  48. Notice that the form of this IF statement is still correct:
  49. IF condition THEN statement;
  50. The statement, in this case, is a Block statement, rather than a
  51. simple statement.
  52. ##### DO:
  53. Take a look at the program called TEST1.
  54. The sample programs which begin with the word TEST are provided
  55. to make it quicker for you to test new statements and concepts.
  56. The program, TEST1, has some integer variables and character
  57. variables declared and the main BEGIN END. All you need to do to
  58. test a statement, or group of statements, is edit them into the
  59. test program and run the program.
  60. ##### DO:
  61. Insert the following statements between the BEGIN and END of
  62. program TEST1:
  63. Write('Enter two numbers ');
  64. ReadLn(I, J);
  65. WriteLn('I=', I, ' J=', J);
  66. Run the program.
  67. ##### DO:
  68. Between the ReadLn and WriteLn statements you just entered, add
  69. the IF statement to swap I and J if I is larger:
  70. IF I > J
  71. THEN
  72. BEGIN
  73. Temp := I;
  74. I := J;
  75. J := Temp;
  76. END;
  77. Run the program several times using several pairs of input
  78. numbers to test the program. Does the "swap" work right?
  79. Œ
  80. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 34
  81. ##### DO:
  82. Remove the BEGIN and END in the IF statement and run the program
  83. several times.
  84. Does the "swap" still work right?
  85. Without the BEGIN and END to make the three statements appear as
  86. one, the statements "appear" as follows to Pascal:
  87. IF I > J
  88. THEN
  89. Temp := I;
  90. I := J;
  91. J := Temp;
  92. Only the statement, Temp := I, is controlled by the IF condition.
  93. 2. CASE statement.
  94. First, a bit of review from an earlier lesson:
  95. Program sequencing is done in Pascal with
  96. (1) Simple Sequence, one statement follows another,
  97. (2) Selection Structures,
  98. IF for one-way and two-way selection,
  99. CASE for many-way selection,
  100. (3) Repetition Structures,
  101. REPEAT statement,
  102. WHILE statement,
  103. FOR statement.
  104. The CASE statement is useful when there are more than two actions
  105. or statement sequences needed. The form of the CASE statement:
  106. CASE variable OF
  107. value_1 : Statement_1;
  108. value_2 : Statement_2;
  109. .
  110. .
  111. .
  112. value_n : Statement_n;
  113. ELSE
  114. Statement;
  115. END; {CASE}
  116. Œ
  117. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 35
  118. Note the following:
  119. The variable must be a simple type such as Integer, CHAR,
  120. BOOLEAN. (REAL is not allowed).
  121. The values used to determine which Statement to execute must be
  122. of the same type as the variable.
  123. The values may be a constant, a list of constants, or a subrange
  124. such as 1..10 (all integers from 1 to 10).
  125. How it works:
  126. If the variable has a value of "value_1" then Statement_1 is
  127. executed. If "value_2" then Statement_2, . . .
  128. If the value of the variable matches none of the values, the
  129. statement following the ELSE is executed.
  130. There must be an END to mark the end of the CASE statement.
  131. (It's a good idea to add the comment {CASE} after the END).
  132. ##### DO:
  133. Use the editor to examine PROG8.
  134. This is the same problem as in the previous lesson, with a bit
  135. more programming flexibility derived from the CASE statement and
  136. the block statements.
  137. Notice that the list of variables in the CASE statement allow
  138. appropriate responses for acceptable responses: A, a, B, b, C, c
  139. correct responses: D, d
  140. unacceptable responses: anything else.
  141. ##### DO:
  142. Modify the CASE statement in PROG8 to accept C as the best
  143. answer.
  144. Run the program. Did it work?
  145. What message appears when D is entered?
  146. Œ