HP Fortran Programmer's Reference (September 2007)

HP Fortran statements
WHERE (statement and construct)
Chapter 10 493
array-assignment-statement
must be an intrinsic array assignment statement; no
defined assignment statements are permitted.
Each elemental operation in
array-assignment-statement
is masked by the array
logical expression.
The elements of the arrays that are used in the WHERE part (the assignments after the
WHERE keyword) are those corresponding to the true elements of the array logical
expression. The elements of the arrays that are used in the ELSEWHERE part (the
assignments after the ELSEWHERE keyword and before the END WHERE keywords) are those
corresponding to the false elements of the array logical expression.
Each
array-assignment-statement
executes in the order in which it appears in both the
WHERE and ELSEWHERE part of the WHERE construct.
In a WHERE construct, only the WHERE statement may be a branch target statement.
Examples
REAL, DIMENSION(150) :: a, recip_a
REAL(DOUBLE), DIMENSION(10,20,30) :: b, sqrt_b
! Assign 1.0/a to recip_a only where a is nonzero
WHERE( a /= 0.0 ) recip_a = 1.0 / a
WHERE( b .GE. 0.0 )
! Assign to sqrt_b only where b is nonnegative
sqrt_b = SQRT(b)
ELSEWHERE ! Set sqrt_b to 0.0 where b is -ve.
sqrt_b = 0.0
END WHERE
INTEGER, DIMENSION(no_of_tests, student):: score
CHARACTER, DIMENSION(no_of_tests, student) :: letter_grade
! Assign letter grades for numeric scores
WHERE( score >= 92 ) letter_grade = 'A'
WHERE( score >= 82 .AND. score <= 91 ) letter_grade = 'B'
WHERE( score >= 72 .AND. score <= 81 ) letter_grade = 'C'
WHERE( score >= 62 .AND. score <= 71 ) letter_grade = 'D'
WHERE( score >= 0 .AND. score <= 61 ) letter_grade = 'E'
In the next example, the arrays values, delta, and count must all be of the same shape:
WHERE (ABS(values) .LT. 10.0)
values = ABS(values) + delta
count = count + 1
ELSEWHERE
values = 0
count = count + 1
ENDWHERE