The WHERE construct controls which elements of an array will be affected by a block of assignment statements. This is also known as masked array assignment.
The WHERE statement signals the beginning of a WHERE construct.
The ELSE WHERE statement controls assignment of each element of a WHERE statement's logical mask that evaluates to false, and each element of the ELSE WHERE's logical mask that evaluates to true. It executes a block of assignment statements for each of the corresponding elements in an assignment expression.
The END WHERE statement signals the end of the innermost nested WHERE construct.
WHERE (mask-expr) [assignment-stmt] [assignment-stmt] ... [ELSEWHERE (mask-expr)] [assignment-stmt] [assignment-stmt] ... [ELSE WHERE] [assignment-stmt] [assignment-stmt] ... END WHERE
mask-expr is a LOGICAL expression.
assignment-stmt is an assignment statement.
mask-expr is evaluated at the beginning of the masked array assignment and the result value governs the masking of assignments in the WHERE statement or construct. Subsequent changes to entities in mask-expr have no effect on the masking.
The variable on the left-hand side of assignment-stmt must have the same shape as mask-expr.
When assignment-stmt is executed, the right-hand side of the assignment is evaluated for all elements where mask-expr is true and the result assigned to the corresponding elements of the left-hand side.
If a non-elemental function reference occurs in the right-hand side of assignment-stmt, the function is evaluated without any masked control by the mask-expr.
assignment-stmt must not be a defined assignment statement.
Each statement in a WHERE construct is executed in sequence.
If the ELSE WHERE statement does not have a mask expression, it must be the last block of assignment code to appear in the construct.
There can be multiple ELSEWHERE statements with mask-exprs.
Example 1integer :: a(3)=(/1,2,3/) where (a == 2) a=-1 end where
Example 2integer :: a(3)=(/1,2,3/),b(3)=(/3,2,1/) where (b > a) a=b ! a is assigned (/3,2,3/) else where(b == a) ! (.NOT. b>a) .AND. b==a b=0 ! b is assigned (/3,0,1/) elsewhere(a == 2) ! (.NOT.b>a).AND.(.NOT.b==a).AND.a==2 a=a+1 ! b==a got to these elements first elsewhere ! (.NOT. b>a) .AND.(.NOT.b==a).AND. (.NOT.a==2) b=-1 ! b is assigned (/3,0,-1/) end where write(*,*) a,b