12.1
Conditional
expressions
Often you will want to instruct a macro to do something only if a
specific condition is true (decision-processing); or to do one
thing if a condition is true, and something else if it is not
(branching); or to continue to do something as long as a
condition is true (looping). These conditions are inserted in
the macro as conditional expressions, which the macro
evaluates to determine if they are true or false.
PerfectScript provides a number of operators for creating
conditional expressions. These operators set up either an
equality or an inequality relationship between the operands.
This relationship can then be tested to see whether it is true or
false, and that in turn can determine whether commands are
executed or not.
In the simplest test, you compare a variable against another
variable, or a variable against a string or a number. But you
can also test expressions made up of variables, numbers, and
strings. System variables can be used like other variables in
comparisons.
12.2
The equality operator
The equality operator is used to determine if one expression is
equal to another. If so, the condition is true. The equality
operator is "=" (equal sign). Examples:
| x = y |
|
true if the
value of variable x
is equal to the
value of variable y
|
| x
= 100 |
|
true if the value of
variable x is equal to 100 |
| x
= "foo" |
|
true if variable
x has been assigned the string "foo" |
| ?Page = 5 |
|
true if the cursor is
on page 5 |
| x
+ 10 =
50 |
|
true if the value of
x is 40 |
| x =
y
- 5 |
|
true if x is 5 less than y |
| x
=
?SelectedText |
|
true if x has been assigned a string which is the same as the
currently selected text
|
Note that for strings to be equal they must match exactly,
i.e., character for character. Upper-case letters, lower-case
letters, numerals, spaces, punctuation, etc. are all different
characters. Remember that strings must be enclosed in quotes.
12.3
The inequality operators
The inequality operators are:
| > |
|
greater
than |
| >= |
|
greater than or equal
to |
| < |
|
less than |
| >= |
|
less than or equal
to |
| <> |
|
not equal
to |
Examples:
| x > 15 |
|
true if the
value of variable x
is greater than
15 |
| y
< 20 |
|
true if the value of
variable y is less than 20 |
| x
>= 100 |
|
true if the value of
variable x is greater than or equal to
100 |
| y
<= x |
|
true if the value of
variable y is less than or equal to the
value of variable x |
| x
<> 50 |
|
true if the value of
variable x is anything other than
50 |
| x >
y+5 |
|
true if the value of
variable x is more than 5 greater than the
value of variable y |
You can use the inequality operators to compare strings;
strings have a value based on the collating sequence of the
WordPerfect character set (the "number" of each character).