02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Expressions and assignment<br />

Assignment<br />

WHERE (a > b)<br />

a = b<br />

b = 0<br />

ELSEWHERE<br />

b = a<br />

a = 0<br />

END WHERE<br />

is evaluated as if it was specified as:<br />

mask = a > b<br />

WHERE (mask) a = b<br />

WHERE (mask) b = 0<br />

WHERE (.NOT.mask) b = a<br />

WHERE (.NOT.mask) a = 0<br />

Only assignment statements may appear in a WHERE block or an ELSEWHERE block. Within a<br />

WHERE construct, only the WHERE statement may be the target of a branch.<br />

The form of a WHERE construct is similar to that of an IF construct, but with this important<br />

difference: no more than one block of an IF construct may be executed, but in a WHERE<br />

construct at least one (and possibly both) of the WHERE and ELSEWHERE blocks will be executed.<br />

In a WHERE construct, this difference has the effect that results in a WHERE block may feed into,<br />

and hence affect, variables in the ELSEWHERE block. Notice, however, that results generated in<br />

an ELSEWHERE block cannot feed back into variables in the WHERE block.<br />

The following example score2grade.f90 illustrates the use of a masked assignment to find the<br />

letter-grade equivalent for each test score in the array test_score. Todothesameoperation<br />

without the benefit of masked array assignment would require a DO loop iterating over the<br />

array either in an IF-ELSE-IF construct or in a CASE construct, testing and assigning to each<br />

element at a time.<br />

Example 5-2 score2grade.f90<br />

PROGRAM main<br />

! illustrates the use of the WHERE statement in masked array<br />

! assignment<br />

!<br />

! use an array constructor to initialize the array that holds<br />

! the numerical scores<br />

INTEGER, DIMENSION(10) :: test_score = &<br />

(/75,87,99,63,75,51,79,85,93,80/)<br />

! array to hold the equivalent letter grades (A, B, C, etc.)<br />

CHARACTER, DIMENSION(10) :: letter_grade<br />

! because the array arguments are declared in the procedure<br />

! as assumed-shape arrays, the procedure’s interface must<br />

! be explicit<br />

!<br />

INTERFACE<br />

SUBROUTINE convert(num, letter)<br />

100<br />

Chapter 5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!