home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap04.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  10.0 KB  |  293 lines

  1.  
  2.  
  3.  
  4.                                                         Chapter 4
  5.                                   LOGICAL COMPARES AND PRECEDENCE
  6.  
  7.  
  8. WHAT IS A BOOLEAN VARIABLE?
  9. _________________________________________________________________
  10.  
  11. Examine the program named COMPARE.ADA for an      ===============
  12. example of logical compares being used in a very    COMPARE.ADA
  13. trivial way.  We declare and initialize three     ===============
  14. INTEGER type variables for use later then
  15. declare two variables of type BOOLEAN in lines
  16. 14 and 15, with the first one being initialized to TRUE.  A BOOLEAN
  17. type variable has a very limited range of values which can be
  18. assigned to it, namely TRUE or FALSE, and there are no mathematical
  19. operations available for use on variables of type BOOLEAN.
  20.  
  21. Much more will be said about the BOOLEAN type variable later in
  22. this tutorial, but we need a basic understanding of a boolean
  23. variable in order to study program control in the next chapter.
  24.  
  25. Lines 19 and 23 illustrate how you can assign a value of either
  26. TRUE or FALSE to a BOOLEAN variable.  These illustrate literal
  27. assignment in much the same way that a literal value can be
  28. assigned to an INTEGER type variable.  Because we wish to display
  29. BOOLEAN values, we instantiate a copy of the generic package
  30. Enumeration_IO for the BOOLEAN type.  Once again, we will study the
  31. details of this later in the tutorial.  This package makes it
  32. possible to output BOOLEAN values in lines 21 and 25.
  33.  
  34.  
  35.  
  36. BOOLEAN ASSIGNMENT STATEMENTS
  37. _________________________________________________________________
  38.  
  39. Lines 28 through 30 are a bit more interesting because they
  40. illustrate assigning a calculated BOOLEAN value to a BOOLEAN
  41. variable.  In line 28, the value of the INTEGER variable One has
  42. the literal 1 added to it and the total is compared to the value
  43. contained in the variable Two.  The expression reduces to the
  44. expression 1 + 1 = 2, and since 1 + 1 is equal to 2, the expression
  45. evaluates to TRUE which is assigned to the BOOLEAN variable Is_It.
  46. The various steps in evaluation are illustrated below for the
  47. student with little or no experience using BOOLEAN expressions in
  48. some other programming language.
  49.  
  50.      Is_It := (One + 1) = Two;
  51.      Is_It := (1   + 1) = 2;
  52.      Is_It := 2 = 2;
  53.      Is_It := TRUE;
  54.  
  55. The single equal sign is the BOOLEAN operator for equality, and if
  56. the two expressions being evaluated are of the same value, a value
  57.  
  58.                                                          Page 4-1
  59.  
  60.                       Chapter 4 - Logical Compares and Precedence
  61.  
  62. of TRUE will result.  If they are not of the same value, the
  63. BOOLEAN result will be FALSE.
  64.  
  65.  
  66. ARE THERE OTHER BOOLEAN OPERATORS?
  67. _________________________________________________________________
  68.  
  69. There are a total of six BOOLEAN operators, and all six will be
  70. illustrated in the next example program.  Line 29 in the present
  71. program illustrates use of one of the others, the inequality
  72. operator.  This statement says, if One is not equal to Two then
  73. assign the value of TRUE to the BOOLEAN variable Is_It, otherwise
  74. assign a value of FALSE to the BOOLEAN variable Is_It.  Note that
  75. regardless of the result, a value will be assigned to the BOOLEAN
  76. variable.  Finally, the "greater than or equal" operator is
  77. illustrated in use in line 30.  This is a rather silly program
  78. since none of the results are used, but it is meant to illustrate
  79. just what a BOOLEAN variable is and how to use it.  Compile and run
  80. this program before continuing on to the next example program.
  81.  
  82.  
  83.  
  84. ADDITIONAL TOPICS ON BOOLEAN EVALUATION
  85. _________________________________________________________________
  86.  
  87. Examine the program named COMPARES.ADA and you   ================
  88. will see all six BOOLEAN operators in use.  We     COMPARES.ADA
  89. define two INTEGER and three BOOLEAN type        ================
  90. variables in the declaration part of the
  91. program, and begin the executable part with some
  92. compare examples in lines 17 and 18.  It should be clear that in
  93. line 18, Index can not be less than itself, so the result is FALSE.
  94.  
  95. Lines 21 through 26 illustrate the use of all six BOOLEAN operators
  96. which are available in Ada and you should have no trouble
  97. understanding this list.
  98.  
  99.  
  100.  
  101. THE BOOLEAN and OPERATOR
  102. _________________________________________________________________
  103.  
  104. Lines 29 through 32 illustrate composite BOOLEAN operators using
  105. the reserved words and, or, not, and xor.  The statement in line
  106. 29 says that
  107.  
  108.          if Index = 12
  109.      and if Count = 12
  110.      and if Truth currently has the value TRUE
  111.      and if TRUE    (which is always TRUE)
  112.             then assign TRUE to Question
  113.             otherwise assign FALSE to Question.
  114.  
  115.  
  116.  
  117.                                                          Page 4-2
  118.  
  119.                       Chapter 4 - Logical Compares and Precedence
  120.  
  121. Using the freeform available in an Ada program to write the
  122. previous sentence, can help in understanding the sentence.  The
  123. point to be illustrated is that you can combine as many BOOLEAN
  124. expressions as you desire to do a particular job.
  125.  
  126.  
  127.  
  128. THE BOOLEAN or OPERATOR
  129. _________________________________________________________________
  130.  
  131. Using the expanded freeform available in Ada, the statement in line
  132. 30 says that
  133.  
  134.            if Index is not equal to 12
  135.         or if FALSE    (which is always FALSE)
  136.         or if Count is greater than 3
  137.         or if Truth currently has the value of TRUE
  138.               then assign TRUE to Question
  139.               otherwise assign FALSE to Question.
  140.  
  141.  
  142.  
  143. THE BOOLEAN not OPERATOR
  144. _________________________________________________________________
  145.  
  146. The expression in line 31 illustrates the use of these two
  147. operators combined in a slightly more complex way using parentheses
  148. to properly group the expressions.  A new operator appears here,
  149. the not which simply says to reverse the meaning of the BOOLEAN
  150. operator which it precedes.
  151.  
  152.  
  153.  
  154. THE BOOLEAN xor OPERATOR
  155. _________________________________________________________________
  156.  
  157. Line 32 illustrates the use of the "exclusive or" operator which
  158. says that the result will be TRUE if one and only one of the
  159. operands are TRUE, and FALSE if both operands are TRUE, or both
  160. operands are FALSE.  A good illustration of this operation would
  161. be a hall light with a switch at both ends of the hall.  If one of
  162. the switches is up, the light is on, but if both are up or both are
  163. down, the light is off.
  164.  
  165.  
  166.  
  167. FULL EVALUATION OF BOOLEAN EXPRESSIONS
  168. _________________________________________________________________
  169.  
  170. The way the expressions are written in lines 29 through 32, all of
  171. the expressions will be evaluated when the statement is executed.
  172. If, in the case of line 29, the value of Index is not 12, then the
  173. final result will be FALSE no matter what the rest of the
  174. expressions are and it would be a waste of time to evaluate them.
  175.  
  176.                                                          Page 4-3
  177.  
  178.                       Chapter 4 - Logical Compares and Precedence
  179.  
  180. Ada will continue blindly across the entire line evaluating all of
  181. the expressions and wasting time since it should know the final
  182. result based on the first comparison.  There is a way to tell it
  183. to stop as soon as it knows the final answer, through use of the
  184. short circuit operators.
  185.  
  186.  
  187.  
  188. WHAT ARE "SHORT CIRCUIT OPERATORS"?
  189. _________________________________________________________________
  190.  
  191. If you study line 35, you will see that if Index is equal to Count,
  192. we will be dividing the constant 9 by zero in the second part of
  193. the expression.  By adding the reserved word then to the and
  194. operator we have a short circuit operation, which means as soon as
  195. the system knows the final outcome, the remaining operations are
  196. short circuited and not executed.  In the present example, if Index
  197. is equal to Count, the first term is FALSE and there is no need to
  198. continue since the second term is to be anded with the FALSE
  199. resulting in FALSE no matter what the second term is.  Division by
  200. zero is avoided in this case because the division is not attempted.
  201.  
  202. In the same manner, line 36 illustrates the short circuit or
  203. operator which is obtained by adding the reserved word else.  In
  204. this case, if Index is equal to Count, the result will be TRUE
  205. regardless of what the second term is, so the second term is not
  206. evaluated and division by zero is avoided.  Line 37 is identical
  207. to line 36 but illustrates the use of parentheses to make the logic
  208. a little easier to read.
  209.  
  210.  
  211.  
  212. ORDER OF PRECEDENCE
  213. _________________________________________________________________
  214.  
  215. The order of precedence of operators is given by the following
  216. list.
  217.  
  218.      **   not  abs        -- Highest precedence
  219.      *    /    mod  rem   -- Multiplying operators
  220.      +    -               -- Unary operators
  221.      +    -    &          -- Binary adding operators
  222.      =    /=   <          -- Relational operators
  223.      <=   >    >=         -- Relational operators
  224.      in   not in          -- (same precedence)
  225.      and  or  xor         -- Logical operators
  226.      and  then  or else   -- (same precedence)
  227.  
  228. The LRM has a complete list of the operators and the details of the
  229. order of precedence in section 4.5.  If there is any question as
  230. to the order of precedence, you should group expressions together
  231. with parentheses since they have the absolute highest precedence.
  232. A future reader of your program will know exactly what your program
  233. is doing.
  234.  
  235.                                                          Page 4-4
  236.  
  237.                       Chapter 4 - Logical Compares and Precedence
  238.  
  239.  
  240. Be sure to compile and execute this program.  Note that we have not
  241. yet studied the &, the in, and the not in operators but will soon.
  242.  
  243.  
  244.  
  245. PROGRAMMING EXERCISE
  246. _________________________________________________________________
  247.  
  248. 1.   Add some output statements to both example programs to see
  249.      that the results are as predicted.  This will give you
  250.      experience using the boolean output statement.
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.                                                          Page 4-5