A good deal of care is required when working with complicated expressions. It is easy to omit parentheses or operators inadvertently. If an operator or a single parenthesis is omitted, a syntax error will be detected. If a pair of parentheses is omitted, the expression, although syntactically correct, may compute the wrong value.
Sometimes it is beneficial to break a complicated expression into subexpressions that are separately assigned to temporary variables and then to manipulate these temporary variables. For example, it is easier to write correctly the three assignment statements
Temp1 := Sqrt( X + Y); Temp2 := 1 + Temp1; Z := Temp1 / Temp2;than the single assignment statement
Z := Sqrt( X + Y) / ( 1 + Sqrt( X + Y));which has the same effect. Using three assignment statements also happens to be more efficient in this case because the square root operation is performed only once; it is performed twice in the single assignment statement above.
Be careful to use the correct type of operands with each operator. The
arithmetic operators can be used only with operands of type
Integer
or Float
or subtypes of these. The relational
operators can be used with any scalar data type. The Boolean
operators can be used only with type Boolean
operands.
Make sure that an operator does not have incompatible type operands. The
Boolean
expression
3 /= '3'is invalid because it compares an integer to a character value. All operators require compatible operands; make sure that you supply the right type operand to mathematical functions. An example is
Sqrt
, whose argument must
be Float
and nonnegative.
Remember that in a CASE
statement, there must be enough
CASE
choices to exhaust every possible value of the
CASE
selector variable or expression. If there are not, a
WHEN OTHERS
choice must be provided; otherwise a compilation error
will result. If you find that you are writing a large number of WHEN
OTHERS
choices, your case selector variable may be of an inappropriate
type (e.g., Integer
instead of a subtype or enumeration type).
Copyright © 1996 by Addison-Wesley Publishing Company, Inc.