TL09.TXT 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 36
  2. TURBO-LESSON 9: FOR STATEMENT
  3. OBJECTIVES - In lesson 9, you will learn about:
  4. 1. Using ":n" to specify field width in Write statements
  5. 2. FOR statement
  6. 1. Using ":n" to specify field width in Write statements.
  7. Spacing of output in previous lessons has been done using spaces
  8. in string constants, such as, 'I= '. The space after the = will
  9. be printed as part of the string.
  10. There is another way to add spaces. When listing the items to
  11. write in a Write or WriteLn statement, ":n" can be added to
  12. specify how many characters the item is to occupy in the output.
  13. For example:
  14. WriteLn(I:4);
  15. The value of I would be printed in a space 4 characters wide.
  16. ##### DO:
  17. Run Program TEST1 several times with the following statements in
  18. the main part of the program:
  19. ReadLn(I);
  20. WriteLn('[', I:4, ']');
  21. Look closely at the output. Are the numbers "left-justified" or
  22. "right-justified"?
  23. If I has a value of 23, and 23 prints in columns 1 and 2 of the
  24. 4 column field, it is left-justified. If the 23 appears in
  25. columns 3 and 4 of the 4 colunm field, it is right-justified.
  26. What happens if you enter a number that is too large to fit the
  27. field specified?
  28. ##### DO:
  29. Run the program several times using the following values for I:
  30. 123, 1234, -1234, 12345
  31. Œ
  32. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 37
  33. 2. FOR statement.
  34. In the previous lesson, you were reminded of the various ways to
  35. control the sequencing of program actions. One of the REPETITION
  36. structures is the FOR statement. The forms of the statement are:
  37. FOR variable := lowvalue TO highvalue DO
  38. Statement; {May be a block statement with BEGIN END}
  39. FOR variable := highvalue DOWNTO lowvalue DO
  40. Statement;
  41. ##### DO:
  42. Run PROG9 several times to see how it works.
  43. Experiment with both positive and negative numbers.
  44. What happens if High is smaller than Low?
  45. ##### DO:
  46. Run PROG9 using 5 for the Low, 3 for the High?
  47. What was the result?
  48. Also try 5 for both values. How many times is the loop repeated
  49. if Low = High?
  50. What results would you expect if you entered 3.4 for Low and 5.3
  51. for High?
  52. ##### DO:
  53. Run the program with the values 3.4 and 5.3.
  54. Were you correct in predicting the outcome?
  55. Can constants be used instead the variables Low and High?
  56. ##### DO:
  57. Edit the FOR statement in PROG9 as follows:
  58. FOR Index := 1 to 5 DO
  59. Run the Program.
  60. Is it possible to use expressions for Low and High?
  61. ##### DO:
  62. Change the FOR statement to:
  63. FOR Index := Low + 1 TO High DO
  64. Run the Program.
  65. Œ