WHERE construct

Standard: F77 F90 F95 F2003 F2008 F2018 Example program


<Syntax>

[ construct-name : ] WHERE ( expression )
      where-block
[ ELSEWHERE ( expression ) [ construct-name ]
      where-block ]...
[ ELSEWHERE [ construct-name ]
      where-block ]
END WHERE [ construct-name ]

where-block ::= [ where-block-stmt ]...

where-block-stmt ::= assignment-stmt
| where-stmt
| where-construct

The expressions must all be logical array expressions with the same shape. The assignment statements must all have array variables as the left-hand-side with the same shape as the expressions. The nested WHERE statements and constructs must have mask expressions with the same shape as the initial WHERE construct statement.

The ELSEWHERE keyword may be spelled ELSE WHERE. The blank between the END and WHERE keywords is optional. If the WHERE construct is named, the END WHERE statement must specify the same construct name. The optional construct name on the ELSEWHERE statement is only allowed when the construct is named, and must be the same as on the WHERE construct statement.

<Semantics>

The WHERE construct performs a sequence of masked assignment statements under the control of a logical mask. Elements of a variable being assigned to in the where-block of a WHERE construct statement are only assigned to if the corresponding element of the mask expression is true; for a variable in the where-block of a masked ELSEWHERE statement, only elements for which this mask is true and all previous masks were false are assigned to; and for a variable in the where-block of a simple ELSEWHERE statement, only elements for which all previous masks were false are assigned to.

For nested WHERE construct and statements, the conditions must be satisfied both for the outer WHERE and the inner WHERE.

<Related>

Array Assignment, Array Constructor, Array Section, DIMENSION attribute, ELSEWHERE statement, END WHERE statement, FORALL construct, FORALL statement, WHERE statement