TL06.TXT 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 25
  2. TURBO-LESSON 6: CONDITIONAL PROCESSING
  3. OBJECTIVES - You will learn, in this lesson, about:
  4. 1. Selection structures used for conditional processing
  5. 2. IF statement (one-way selection)
  6. 3. IF statement (two-way selection)
  7. 1. Selection structures used for conditional processing.
  8. There are three types of statement sequencing used in PASCAL:
  9. (1) SIMPLE SEQUENCE. One statement follows another with no
  10. branching.
  11. (2) SELECTION STRUCTURES. Based on a condition, supplied by
  12. the programmer, the next statement to execute is chosen from two
  13. alternatives (IF statement) or chosen from many alternatives
  14. (CASE statement).
  15. (3) REPETITION STRUCTURES. A group of program statements may
  16. be repeated more than once, dependent on a condition supplied by
  17. the programmer. The repetition statements are WHILE, REPEAT, and
  18. FOR, included in later lessons.
  19. The Selection statement, IF .. THEN .. ELSE, is illustrated in
  20. this lesson, the CASE statement appears in a later lesson.
  21. Œ
  22. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 26
  23. 2. IF statement (one-way selection).
  24. The one-way IF is really a special case of the two-way IF, where
  25. the second alternative is to do nothing, just go on to the next
  26. statement. The form of the IF statement is:
  27. IF condition THEN statement;
  28. The condition is an expression or comparison which the computer
  29. can evaluate as TRUE or FALSE. Examples of a condition:
  30. 7 < 10 TRUE
  31. I < 10 TRUE, if the memory location named I holds a
  32. value less than 10, otherwise FALSE.
  33. NOT(7 < 10) FALSE (7 < 10 is TRUE, but NOT reverses the
  34. value to FALSE).
  35. ##### DO:
  36. Look at PROG6.
  37. The first IF statement is a one-way selection. If the condition
  38. is true, the WriteLn statement will be executed. If the
  39. condition is false, the WriteLn will be ignored.
  40. Run the program using 0 for the no of computers owned.
  41. Run it again with 1 for the no of computers owned.
  42. The "No Computer!" message should print for 0 computers owned,
  43. but not print for 1 computer owned.
  44. ##### DO:
  45. Examine the last IF in the program.
  46. This is a one-way IF with a slightly more complicated condition.
  47. The condition contains an integer expression, (Want - Have). The
  48. computer first evaluates the integer expression to get a number
  49. to compare with the 2 on the right side of the ">".
  50. ##### DO:
  51. Run the program several times with different input to see the
  52. effect of this IF.
  53. Also note the misspelled "Aren''t" in the message. The double
  54. apostrophe is used in the message to represent a single
  55. apostrophe. If a single apostrophe were used, it would appear to
  56. be the end of the message.
  57. Œ
  58. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 27
  59. 3. IF statement (Two-way selection).
  60. The two-way IF causes one of two alternative statements to be
  61. executed, based on whether the condition is TRUE or FALSE. The
  62. form of the statement is:
  63. IF condition THEN Statement_1 ELSE Statement_2;
  64. If the condition is TRUE, the statement following the THEN is
  65. executed. If the condition is FALSE, the statement following the
  66. ELSE is executed.
  67. ##### DO:
  68. Look at the second IF in PROG6.
  69. This is a two-way IF. A congratulations message is printed if
  70. the condition is true, condolences if false.
  71. The condition is (Have >= Want). This condition is true if the
  72. number you enter for computers owned is greater than or equal to
  73. the number you enter for computers you would like to have.
  74. ##### DO:
  75. Run the program several times, experimenting with various input
  76. values.
  77. ##### DO:
  78. Try your hand at writing an IF statement to do the following:
  79. If the number of computers owned is more than the number of
  80. computers wanted, print a message 'Send extra computers to
  81. (put your name here?) '.
  82. Œ