HP Fortran Programmer's Reference (September 2007)

Expressions and assignment
Assignment
Chapter 4 103
INTEGER :: num(:)
CHARACTER :: letter(:)
END SUBROUTINE convert
END INTERFACE
PRINT *, 'Numerical score:', test_score
CALL convert(test_score, letter_grade)
PRINT '(A,10A3)', ' Letter grade: ', letter_grade
END PROGRAM main
SUBROUTINE convert(num, letter)
! declare the dummy arguments as assumed-shape arrays
INTEGER :: num(:)
CHARACTER :: letter(:)
! use the WHERE statements to figure the letter grade
! equivalents
WHERE (num >= 90) letter = 'A'
WHERE (num >= 80 .AND. num < 90) letter = 'B'
WHERE (num >= 70 .AND. num < 80) letter = 'C'
WHERE (num >= 60 .AND. num < 70) letter = 'D'
WHERE (num < 60) letter = 'F'
END SUBROUTINE convert
Here are the command lines to compile and execute the program, along with the output from
a sample run:
$ f90 score2grade.f90
$ a.out
Numerical score: 75 87 99 63 75 51 79 85 93 80
Letter grade: C B A D C F C B A B
The next example is a subroutine that uses the WHERE construct to replace each positive
element of array a by its square root. The remaining elements calculate the complex square
roots of their values, which are then stored in the corresponding elements of the complex
array ca. In the ELSEWHERE part of the construct, the assignment to array a should not appear
before the assignment to array ca; otherwise, all of ca will be set to zero.
SUBROUTINE find_sqrt(a, ca)
REAL :: a(:)
COMPLEX :: ca(:)
WHERE (a > 0.0)
ca = CMPLX(0.0)
a = SQRT(a)
ELSEWHERE
ca = SQRT(CMPLX(a))
a = 0.0
END WHERE
END SUBROUTINE find_sqrt