home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


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 Expressions

The 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 Operators

Turbo 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)

Drill 2-3

Write boolean expressions to express the following conditions:

1.  A is less than 55.5
2.  x is equal to y, or x is greater than or equal to z
3.  either x=40, or y=80; or both
4.  either x=40, or y=80; but not both

Precedence of Operators

As 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.

Table 2-5 Precedence of Pascal Operators

Operator Precedence

NOT Priority 1 (highest)
* / DIV MOD AND Priority 2
+ - OR (XOR in Turbo Pascal) Priority 3
= > < >= <= <> Priority 4 (lowest)

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

1.  In this chapter you learned the four standard data types:
  INTEGER
  REAL
  CHAR
  BOOLEAN
2.  You also learned the additional numeric types of Turbo Pascal:
I.  Integers:
  SHORTINT
  BYTE
  INTEGER
  WORD
  LONGINT
II.  Reals
  SINGLE
  REAL
  DOUBLE
  EXTENDED
  COMP
3.  You learned the standard arithmetic functions, classified into three groups:
Conversion:
  ROUND
  TRUNC
Trigonometric:
  ARCTAN
  COS
  SIN
Miscellaneous:
  ABS
  EXP
  LN
  SQR
  SQRT
4.  You also learned some additional arithmetic functions from Turbo Pascal, such as:
  FRAC
  INT
  RANDOM
5.  You can now write mathematical expressions using arithmetic operators and functions.
6.  You are now familiar with four functions used to manipulate characters:
  CHR
  ORD
  PRED
  SUCC
7.  You learned some of the features of text string variables in standard Pascal, and you know that such variables are defined as PACKED ARRAYS OF CHAR. In extensions such as Turbo Pascal and UCSD Pascal, the type STRING was added to the language along with other features and functions.
You learned the STRING function:
LENGTH

which is used to measure the dynamic length of a string.
8.  Using the arithmetic, relational, and boolean operators, you learned how to build simple and compound boolean expressions and how to use the type BOOLEAN.
You know as well the boolean operators:
  NOT
  AND
  OR

and you can express the exclusive OR in two ways:
  using the relational operator {<>
  using the Turbo Pascal operator XOR
9.  Finally, you had one last tour of Pascal operators and learned about their relative precedence.


Previous Table of Contents Next


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.