The Queen's University of Belfast

Parallel Computer Centre
[Next] [Previous] [Top]
Logical & comparison expressions
Logical & comparison expressions
Topics
- Relational operators.
- Logical expressions.
- Character comparison.
Relational operators
Form
- Relational operators are used to compare two objects and are of the form:
object1 rel_operator oblect2
- Where
rel_operator
may be one of the following operators:
<
less than,
<=
less than or equal to,
>
greater than,
>=
greater than or equal to,
==
equal to,
/=
not equal to.
- The result of a comparison is a default logical value,
.TRUE.
or .FALSE.
Example
LOGICAL :: test, ntest(5)
INTEGER :: age, my_age, your_age(5)
CHARACTER(len=5) :: name
...
test = 5 < 6 !true
test = 5 <= 6 !true
test = 5 > 6 !false
test = 5 => 6 !false
test = 5 == 6 !false
test = 5 /= 6 !true
...
ntest = your_age > 34
test = your_age(1) /= my_age
test = name == `Smith'
test = (age*3) /= your_age(3)
The objects compared may be variables, constants, array elements (numeric or character) or expressions.
Logical expressions
Form
- Logical expressions may be combined using logical operators.
expression1 logical expression2
- Where
logical
may be one of the following operators:
- .AND.
- .OR.
- .NOT.
- .EQV.
- .NEQV.
- The result of a logical expression is a default logical value,
.TRUE.
or .FALSE.
Example
LOGICAL :: test, part1, part2
...
part1 = 5*3 > 12 !true
part2 = 6*2 < 12 !false
...
test = part1 .AND. part2 !false
test = part1 .OR. part2 !true
test = .NOT. part 1 !false
test = .NOT. part2 !true
test = part1 .EQV. part2 !false
test = part1 .NEQV. part2 !true
...
Character comparison
Certain rules are applied when comparing characters or strings (is `A' greater than `B'?)
- String comparison requires strings of the same length. When comparing strings of different lengths, the shorter is padded with blanks (right side).
- The comparison is from left to right.
- Comparison is character by character.
- The comparison ends either when a difference is found or the end of the string is reached.
The collating sequence
- Character comparison follows a collating sequence, typically:
- 0 < 1 < 2 < ... < 9
- A < B < ... < Z
- a < b < ... < z
- ANSII standard: blank < numerals < upper case < lower case.
- Example:
test = `Alexis' > `Alex'
- The right string becomes
`Alex '
- The first four characters are the same...
- character `
i
' is greater than blank, test=.TRUE.
Portability issues
- The collating sequence is machine dependent!
- Intrinsic functions for string comparison, based on the ASCII standard, are available:
LLT(str1, str2) !str1 < str2
LLE(str1, str2) !str1 <= str2
LGE(str1, str2) !str1 > str2
LGT(str1, str2) !str1 >= str2
These should be used where ever possible.
[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