TL04.TXT 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 16
  2. TURBO-LESSON 4: DECLARATIONS, INPUT
  3. OBJECTIVES - In Lesson 4 you will learn about:
  4. 1. The DECLARATIONS part of a program
  5. 2. VAR declaration
  6. 3. Input using the ReadLn statement
  7. 4. Integer variables
  8. 1. The DECLARATIONS part of a program.
  9. You learned in the previous lesson that there are two main parts
  10. to any PASCAL program: DECLARATIONS, and MAIN BODY. The various
  11. entries in the DECLARATIONS section define the data items used in
  12. the processing in the MAIN BODY. Not all declaration entries
  13. will occur in every program, but only the ones needed to support
  14. the processing.
  15. The various types of declaration entries will be introduced as
  16. needed in the sample programs. Only the VAR entry will be used
  17. in this program.
  18. 2. VAR declaration.
  19. All variables, (spelled A-L-L, no exceptions), must be defined
  20. before they are referenced by processing statements. The VAR
  21. entry is used to define variables. The form of the entry is:
  22. variable-name : type;
  23. The variable-name may be one or several variable-names separated
  24. by commas. The type may be a predefined type, such as Integer,
  25. or a type you have constructed useing the predefined types. The
  26. colon must occur between the variable-name(s) and the type.
  27. Extra spaces are acceptable to allow more readable format. Below
  28. are some VAR entries:
  29. VAR
  30. i,j,k : Integer;
  31. Inkey : Char;
  32. Rate : Real;
  33. Count : Integer;
  34. (The example above includes types not yet discussed, to
  35. illustrate the form of the VAR entry.)
  36. Œ
  37. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 17
  38. ##### DO:
  39. Look at PROG4.
  40. (You know from earlier lessons how to load the program and use
  41. the editor to look at the program.) A segment of PROG4 is:
  42. VAR
  43. Number : Integer;
  44. The VAR entry above defines a variable called "Number" to be of
  45. type "Integer". This means the computer must set up a memory
  46. location large enough to store an integer, which can be accessed
  47. by referring to the name "Number".
  48. Notice the variable, Number, is later referenced in the
  49. processing statements.
  50. ##### DO:
  51. Add an integer variable called "Age" to Prog4. This will be used
  52. later in this lesson. You can either add the new variable to the
  53. declaration of Number
  54. Number, Age : Integer;
  55. or add another declaration
  56. Number : Integer;
  57. Age : Integer;
  58. Compile the program to be sure you haven't made a syntax error.
  59. 3. Input using the ReadLn statement.
  60. ReadLn is the statement used to input variables. The form of the
  61. statement is:
  62. ReadLn(var_1, var_2, . . . ,var_n);
  63. When the statement is executed, the computer will wait for you to
  64. type values for the variables, separated by one or more spaces,
  65. followed by depressing the enter key.
  66. Œ
  67. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 18
  68. Part of PROG4 follows:
  69. BEGIN
  70. Write('Enter a number (no decimals please): ');
  71. ReadLn(Number);
  72. WriteLn; { Display one blank line }
  73. WriteLn('Number: ',Number); { Display the number entered }
  74. END.
  75. The Write statement provides a prompt before the ReadLn accepts
  76. Number. Notice that ReadLn does not provide a "?" or any other
  77. prompt. The programmer must provide any prompting require.
  78. The use of the Write statement for prompting, instead of the
  79. WriteLn, keeps the cursor on the same line so the input will
  80. occur right after the prompt message.
  81. The ReadLn accepts the number you type and stores it in the
  82. memory location which the computer has set aside for the Integer
  83. variable, Number.
  84. ##### DO:
  85. Compile and run PROG4. When prompted, type 12 and enter. Run it
  86. again and enter -34.
  87. ##### DO:
  88. Using the statements of PROG4 as an example, expand the program
  89. to do the following:
  90. (1) Write a prompt to 'Enter your age'.
  91. (2) Read a value for age to be stored in the integer variable,
  92. Age, which you added to the VAR declarations in the previous
  93. section.
  94. (3) Write a message which prints the age entered, in a manner
  95. similar to the way Number was printed.
  96. Run the program.
  97. 4. Integer Variables.
  98. Integers are counting numbers, with no decimal points. They
  99. may be positive or negative. In TURBO the range of Integers
  100. permitted is:
  101. -32768 to +32767
  102. Integers are used for subscripts, indexes, counting, input and
  103. output of such things as counts, limits, menu choices. Decimal
  104. numbers (Real type, discussed later), are needed for such things
  105. as dollar amounts and calculations. When decimal numbers are
  106. not actually needed, Integers should be used since they are
  107. easier to use and take less memory.
  108. Œ
  109. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 19
  110. ##### DO:
  111. Run PROG4 again, this time entering data which will cause errors.
  112. When prompted to enter a number, enter the letter A instead.
  113. What happened?
  114. A message appears:
  115. I/O error 10, PC=287C
  116. Program aborted
  117. Searching
  118. 15 lines
  119. Run-time error position found. Press <ESC>
  120. What this means is as follows:
  121. The program aborted because "I/O error 10" occurred at program
  122. code address, 287C. (Your program may produce a different
  123. program code address.) To find out what "I/O error 10" is, look
  124. in your reference manual in the appendix containing "I/O ERROR
  125. MESSAGES". Error 10 is an "Error in numeric format".
  126. TURBO searched 15 lines before finding where the error occurred.
  127. When you press the ESC key, the editor is activated with the
  128. cursor positioned at the end of the statement which caused the
  129. error. (In this case, it is not the statement which caused the
  130. problem, but the type of data entered.)
  131. ##### DO:
  132. Run the program again, entering too large a number, 88888. Note
  133. that the same error occurs. Run it again with too small a
  134. number, -55555. You may want to experiment with the end-points
  135. of the acceptable integer range: -32768 to 32767. For example,
  136. try entering the values -32767, -32768, -32769.
  137. NOTE: THERE ARE WAYS TO HANDLE ERRORS TO AVOID ABORTING THE
  138. PROGRAM. MORE ABOUT ERROR-HANDLING IN LATER LESSONS.
  139. Œ