TL05.TXT 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 20
  2. TURBO-LESSON 5: INTEGER EXPRESSIONS
  3. OBJECTIVES - In lesson 5 you will learn about:
  4. 1. Assignment statements
  5. 2. Integer expressions
  6. 3. Problems with expressions
  7. 1. Assignment statements.
  8. A large part of computer processing is accomplished by storing
  9. numbers, strings, and other data objects in memory locations in
  10. the computer.
  11. There are several ways to store a value in memory in Pascal:
  12. (1) use an Input statement (ReadLn),
  13. (2) use an Assignment statement,
  14. (3) use a Procedure or function.
  15. The Assignment statement has the following form:
  16. Variable := Expression;
  17. An example: A := B + C;
  18. The way it works: The computer evaluates the expression on the
  19. right side of the replacement operator, :=, and stores the
  20. resulting value in the memory location named by the variable on
  21. the left.
  22. In the example above, the computer would obtain whatever value is
  23. currently stored in the memory location called B, add that value
  24. to the value it finds in the memory location called C, and store
  25. the sum in the memory location, A. If B holds the value 3, and C
  26. holds the value 4, then 7 would be stored in the memory location
  27. called A.
  28. ##### DO:
  29. Use the editor to enter the following short program (omit the
  30. comments, if you like):
  31. PROGRAM ABC;
  32. VAR A, B, C : Integer;
  33. BEGIN
  34. A := 6; {Assign the value 6 to the memory location, A}
  35. B:=7; {Assign 7 to B }
  36. C := A + B; {Add the values in A and B, Store in C }
  37. WriteLn ('A=',A, ' B=',B, ' C=',C);
  38. END.
  39. Run the program.
  40. Œ
  41. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 21
  42. Note that you have just written a complete PASCAL program - from
  43. here on, you will just be adding new features in each lesson to
  44. enable you to write more complicated programs. (It didn't run?
  45. Use the error message to correct any errors. If the error
  46. message doesn't make sense, try looking for misplaced or omitted
  47. semicolons. Also note that the assignment statement uses the
  48. compound symbol, ":=" and not "=".).
  49. 2. Integer Expressions.
  50. Integer expressions are composed of integer variables, constants,
  51. and operators. The operators used with integer expressions are:
  52. +, -, *, div, mod.
  53. The + and - have their usual meaning, addition and subtraction.
  54. The * indicates multiplication.
  55. Division of integer numbers is done with div and mod.
  56. ##### DO:
  57. Examine at PROG5.
  58. Two numbers, which you enter, are added, subtracted, and one is
  59. cubed using the multiply operator, "*". WriteLn is used to print
  60. out the results.
  61. Notice that expressions may be calculated in an assignment
  62. statement,
  63. I_Cubed := I * I * I;
  64. or calculated in a WriteLn statement,
  65. WriteLn('I - J', I - J);
  66. ##### DO:
  67. Run the program.
  68. Note that the ReadLn statement will attempt to read 2 values.
  69. The values should be typed with a space between, not a comma.
  70. Enter the values 2 and 5.
  71. Check all the results. Are they all correct?
  72. ##### DO:
  73. Run the program again, this time entering -3 and 5.
  74. Is everything correct again? Notice the negative cube of -3 is
  75. as expected, -27. (Some incorrect negative cubes will appear a
  76. little later in this lesson.)
  77. Œ
  78. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 22
  79. The division operators, div and mod are not used in PROG5. To
  80. see how they work,
  81. ##### DO:
  82. Just before the END of PROG5, edit in the following statements:
  83. WriteLn('I div J = ', I div J);
  84. WriteLn('I mod J = ', I mod J);
  85. Run the program, entering the values 5 and 3.
  86. Did you get the results expected? Is 5 divided by 3 really 1
  87. and not 1.666?
  88. ##### DO:
  89. Add these two statements at the end of the program:
  90. WriteLn(I,' divided by ', J, ' = ', I div J);
  91. WriteLn(' with a remainder of ', I mod J);
  92. Run the program with the values 5 and 3.
  93. Often, when working with integers, it is useful to know one or
  94. both of these components of the division. If you really want the
  95. decimal result of the division, the slash, /, could be used with
  96. integers.
  97. ##### DO:
  98. Add this statement at the end of the program:
  99. WriteLn('I / J = ', I/J);
  100. Run the program with the values 5 and 3.
  101. Note the result of division using the slash is the usual result.
  102. ##### DO:
  103. Before going on, try adding a few WriteLn statements to the
  104. program to improve the readability of the output.
  105. Œ
  106. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 23
  107. 3. Problems with expressions.
  108. You should be aware of the possibilities for various types of
  109. errors involving expressions. First, an easy to detect error.
  110. ##### DO:
  111. Declare K to be an integer variable.
  112. Add these statements at the end of the program:
  113. K := I / J;
  114. WriteLn('K = ', K);
  115. Run the program. How did it go?
  116. This error, "Type mismatch", is easy to find, since the compiler
  117. finds it. The reason for the type mismatch is that the result of
  118. the division using the "/" is a real number (covered in a later
  119. lesson). The variable, K, is an integer. A real number can't be
  120. stored in an integer memory location. (Why not? One reason: a
  121. real number takes 3 times as much memory space.)
  122. A second type of error, is illustrated in the following:
  123. ##### DO:
  124. Run the program using the values 31 and 5.
  125. Check the results of the cube of I. Is it correct?
  126. O.K., but notice the cube of I, 29791, is approaching the upper
  127. limit of integer variables, 32767.
  128. What will happen if you enter 32 and 5? (The correct cube of 32
  129. is 32768, just 1 too large to fit as an integer.)
  130. Try it!
  131. ##### DO:
  132. Run the program using the values 32 and 5.
  133. The computer, known for its reliability, informs you:
  134. The cube of I = -32768
  135. Now, you know the sign is wrong - positive numbers do not produce
  136. negative cubes. But look at the number, -32768. Correct number
  137. with the wrong sign?
  138. Œ
  139. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 24
  140. ##### DO:
  141. Run the program again with the values 33 and 5.
  142. The cube of I = -29599
  143. Wrong number! Wrong sign! So why does the computer go merrily
  144. on its way - giving you these wrong answers?
  145. The computer is very good at detecting errors in the format of
  146. program statements, missing declarations, wrong punctuation.
  147. There are other types of errors that are more difficult to
  148. detect.
  149. It is up to you, the programmer, to find ways to keep these
  150. errors from going unnoticed. For now, you need to be aware that
  151. these problems can occur. Error detection will be covered in
  152. later lessons.
  153. Œ