|
To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
As mentioned earlier, you may not compare two real values for equality because of their limited precision. In the following program, the difference between the two real variables x and y is tested to see whether it is less than a specific small value Difference, in which case they are considered to be equal. { ------------------------------ figure 2-11 ----------------------------- } { Comparing real values } PROGRAM Compare2(INPUT,OUTPUT); CONST Difference = 0.0001; VAR x, y :REAL; Result:BOOLEAN; BEGIN WRITE('Please enter two real numbers: '); READLN(x, y); Result:= ABS(x - y) < Difference; WRITELN('The difference is ', ABS(x-y):2:6); WRITELN('The comparison is ', Result) END. The following is a sample run: Please enter two real numbers: 4.5 4.50001 The difference is 0.000010 The comparison is TRUE Compound BOOLEAN ExpressionsThe boolean expressions which use relational operators are called simple boolean expressions (in other languages they are called relational expressions). The compound boolean expressions are those which use the boolean operators (also called the logical operators): AND, OR, and NOT. To understand how a compound boolean expression works, consider the example: (x = 4) AND (y < 50) This expression is evaluated TRUE if both conditions x = 4 and y < 50 are TRUE. Now consider the same expression using the operator OR: (x = 4) OR (y < 50) This expression is evaluated as TRUE if any one of the conditions is TRUE. For example, if x contains the value 4, the expression is TRUE regardless of the value of y. The logical operator NOT is used to reverse the value of a boolean expression. Suppose that the boolean variable UnderAge means that the age is less than 18, as in the following statement: UnderAge:= Age < 18; The variable UnderAge will contain the value TRUE if the Age is less than 18. Now the expression: NOT(UnderAge) is evaluated TRUE if the Age is 18 or above. Turbo Pascal OperatorsTurbo Pascal also contains the logical operator XOR, which is called the exclusive OR. It is used as in the following expression: (x = 4) XOR (x = 400) The value of this expression is TRUE if either one of the two conditions (x = 4 or x = 400) is TRUE, but the expression is evaluated as FALSE if both conditions are either TRUE or FALSE. In any implementation of Pascal you can use the operator <> as the exclusive OR. You can write the previous expression as: (x = 4) <> (x = 400) Precedence of OperatorsAs with arithmetic expressions, the precedence of operators should be considered when building a boolean expression (relational or logical). Table 2-5 summarizes the relative precedence of all operators you have used so far.
To understand the effects of precedence, try the boolean expression: x = 4 OR x = 400 Because the OR has a higher precedence level than the equality, this will not compile because it will be interpreted as: x = (4 OR x) = 400 which is not a valid expression. Summary
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |