Comparisons: The IF Command

The usefulness of symbols and lexical functions is enhanced by the ability to take some action based on their values. DCL allows you to compare the value of symbols, lexical functions, and literal data in any combination, and then take some action based on the result.

A basic comparison takes the following format:

     $ IF expression THEN $ command 

Comparison Operators

In order to take some action based on a comparison, you must define which two (or more) things you are comparing and what relationship you are looking for. For example, is one number smaller than another? Are two numbers equal? Is one character string longer than another?

DCL provides several comparison operators that each look for a different relationship between the values being compared. These are as follows:

  • .EQ./.EQS. — Tests whether the values/character strings are equal

  • .GE./.GES. — Tests whether the first is greater than or equal to the second

  • .GT./.GTS. — Tests whether the first is greater than the second

  • .LE./.LES. — Tests whether the first is less than or equal to the second

  • .LT./.LTS. — Tests whether the first is less than the second

  • .NE./.NES. — Tests whether they are not equal

Each of the above comparison operators has two variants: The first compares integer values, and the second compares character strings. Therefore, the .GT. operator compares two integers, and the .GTS. operator compares two character strings.

In addition to the comparison operators, there are a few logical operators. They are as follows:

  • .AND. — Combines the values in a bitwise AND operation

  • .OR. — Combines the values in a bitwise OR operation

  • .NOT. — Negates the value occurring after it

  • () — Specifies items within parentheses are evaluated (simplified) first

For those readers not familiar with Boolean logic, the preceding operators may require some explanation.

Every comparison, no matter how complex, is boiled down to one result: TRUE or FALSE. Under DCL, every odd value is considered TRUE. Even values and zero are considered FALSE.

To illustrate:

     $ WRITE SYS$OUTPUT 4 .EQ. 5     0     $ WRITE SYS$OUTPUT 5 .EQ. 5     1 

In the examples, the expression "4 .EQ. 5" is simplified to a single value, 0, meaning FALSE; 4 does not equal 5. On the other hand, "5 .EQ. 5" is simplified to 1, or TRUE.

The .AND. and .OR. operators return 0 (FALSE) or 1 (TRUE) based on the following rules:

click to expand
Figure 6-1: DCL .AND. and .OR. Operators

You can use .AND. or .OR. to determine whether arguments are TRUE or FALSE in the following way. Parentheses are shown here to make clear the order in which expressions are evaluated. They may be used to force a certain order, but in these particular examples they make no difference.

This example illustrates the .AND. operator:

     $ !     $ ! An AND operation requires that BOTH of its arguments be true     $ ! to produce a TRUE result:     $ !     $ rate = "normal"     $ sex = "Male"     $ age = 24     $ !     $ if (sex .eqs. "Male") .and. (age .lt. 25) then $ rate = "high"     $ !     $ write sys$output -       "This subject will pay ''rate' car insurance rates."     This subject will pay high car insurance rates. 

This example illustrates the .OR. operator:

     $ !     $ ! The OR operator returns TRUE if either or both arguments     $ ! are true:     $ !     $ rate = "normal"     $ sex = "Female"     $ age = 24     $ !     $ if (sex .nes. "Male") .or. (age .ge. 25) then $ rate = "low"     $ !     $ write sys$output -       "This subject will pay ''rate' car insurance rates."     This subject will pay low car insurance rates.     $ ! 

start sidebar
Bitwise Versus Logical Operators

The .AND. and .OR. operators in DCL are actually bitwise operators, but they are often used as logical operators by using only zero and one as inputs. DCL considers zero and all even values FALSE, and all odd values TRUE.

end sidebar



Getting Started with OpenVMS(c) A Guide for New Users
Getting Started with OpenVMS: A Guide for New Users (HP Technologies)
ISBN: 1555582796
EAN: 2147483647
Year: 2005
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net