The Queen's University of Belfast  Parallel Computer Centre

## 5 Logical & comparison expressions

### 5.1 Relational operators

Recall that a logical variables denoted with the keyword LOGICAL, and it can take two logical values(.TRUE. or .FALSE.) which are used to record Boolean information about the variable.

Recall that declaring logical variables is in the following form

`LOGICAL :: guess, date`

and assigning a logical variable is in the following form

`guess = .true.`

`date = (today_date==5)`

if today_date has previously been assigned a value and that value is 5 then date holds .TRUE., otherwise .FALSE. In this section the logical and comparison operators are introduced and how to perform comparisons is illustrated.

More Examples:

`5 < 6 !True`

`5 > 6 !False`

`5 == 6 !False`

`5 /= 6 !True`

`5 <= 6 !True`

`5 >= 6 !False`

`age > 34 !a variable compared with a constant`

`age /= my_age !two variables are compared`

`45 == your_age !a variable can appear in any side`

`name= 'Smith' !characters are allowed`

`alpha(3) /= 33 !array elements are allowed`

`(age*3) /= your_age !expressions are allowed`

### 5.2 Logical expressions

The .AND. logical operator is used to link expressions which evaluate to TRUE only if all given expressions are true, otherwise evaluates to FALSE. Consider the following example: (salary*0.4) .and. (age<45). There are two sub-expressions here. If both are true the expression evaluates to true, otherwise the value false is assigned.

The .OR. logical operator is used to link expressions which evaluate to TRUE only if any of the expressions is true, otherwise evaluates to FALSE. Consider the following example: (name='Dimitris') .or. (name='James') .or. (name='Jim'). Again if the user enters any of the names the expression is given the true value, otherwise the value false is assigned.

The .NOT. logical operator is used to inverts the logical value of an expression. For example true becomes false and vice versa. The form is: .not. (salary*0.4) where the statement enclosed by brackets is assigned a value which in turn is inverted.

The .EQV. logical operator is used to link expressions which evaluate to TRUE only if all expressions have the same logical value (can be true or false), otherwise evaluates to FALSE. For example: (5*3>12) .EQV. (6*2>8) evaluates to TRUE because both sub-expressions take the true value.

The .NEQV. logical operator is used to link expressions which evaluate to TRUE only if at least one of the expressions has a different logical value than the others, otherwise evaluates to FALSE. For example: (5*3>12) .NEQV. (6*2>13) evaluates to TRUE because the first sub-expression is true whereas the second is false.

Comparing real & integer converts the integer to its real equivalent Comparing real & real must be performed with caution because of rounding errors resulting from arithmetic operations. It is advisable to test their difference rather than their actual values. For instance, (a-b<0.005) is better than (a==b).

### 5.3 Character Comparisons

Certain rules have to be obeyed when comparing characters or character strings . (Is A greater than B ?) When one of the character strings has a shorter length, it is filled with blanks (right side) The comparison is character by character

The comparison starts from the left side The comparison terminates either when a difference has been found or the end of the string has been reached if no difference is found the character strings are the same, otherwise terminates with the first encountered difference. Comparing character strings depends on the collating sequence of the machine used. The collating sequence must obey the following rules.

A < B < ... < Z

a < b < ... < z

0 < 1 < 2 ... < 9

digits before A or after Z; or before a or after z blank before letters or digits Rest of characters have no defined position, machine dependant Note that standard does not define if upper case characters come before or after lower case characters

The earliest a character comes in the collating sequence the smaller value it has. Hence, a blank is always smaller than a digit or a letter. An example:

Is 'Alexis' > than 'Alex'?

The right expression is shorter, hence 'Alex' becomes 'Alex ' The first 4 letters are the same - no difference has been found so search continues character i is greater than blank - comparison terminates and the answer is yes because the blank comes before letters! (the earlier a character comes in the collating sequence the smaller value it has)

### 5.4 Portability Issues

Collating sequence is machine dependable.

Intrinsic functions for string comparison are available which are based on the universal ASCII collating sequence:

`LGT(string1, string2) !greater than`

`LGE(string1, string2) !greater than or equal to`

`LLE(string1, string2) !less than or equal to`

`LLT(string1, string2) !less than`

Because the collating sequence might differ from machine to machine one can use one of the above intrinsic functions either to compare strings. More intrinsic functions are available. For example intrinsic functions that identify the position of a character in a sequence in the ASCII or machine collating sequence. Some of them are presented through the exercise sections.

### 5.5 Exercises

1. Given that

`INTEGER :: age=34, old=92, young=16`

what is the value of the following expressions?

`age /= old`

`age >= young`

`age = 62`

`(age==56 .and. old/=92)`

`(age==56 .or. old/=92)`

`(age==56 .or. (.not.(old/=92)))`

`.not. (age==56 .or. old/=92)`

2. What are the values of the following expressions?

`15>23`

`(12+3) <=15`

`(2>1) .and. (3<4)`

`(3>2) .and. (1+2)<3 .or. (4<=3)`

`(3>2) .and. (1+2)<3 .eqv. (4<=3)`

3. Is this true?

`(a<b .and. x<y) .or. (a>=b .and. x>=y) = (a<b .eqv. x<y)`

Re-write the following expressions using different logical operators

`.not. (a<b .and. b<c)`

`.not. (a<b .eqv. x<y)`

4. Determine the logical value of each expression

`"Adam" > "Eve"`

`"ADAM" > "Adam"`

`"M1" < "M25"`

`"version_1" > "version-2"`

`" more" < "more"`

`LGT("Adam","adam")`

`LLT("Me","me")`

`LLT("me","me?"`

[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker