The WHERE statement masks the evaluation of expressions and assignments of values in array assignment statements. It does this according to the value of a logical array expression. The WHERE statement can be the initial statement of the WHERE construct.
(1) >>-+-------------------------+-------WHERE--(--mask_expr--)--+----------------------------+->< '-where_construct_name--:-' '-where_assignment_statement-'
If a where_assignment_statement is present, the WHERE statement is not the first statement of a WHERE construct. If a where_assignment_statement is absent, the WHERE statement is the first statement of the WHERE construct, and is referred to as a WHERE construct statement. An END WHERE statement must follow. See WHERE construct for more information.
If the WHERE statement is not the first statement of a WHERE construct, you can use it as the terminal statement of a DO or DO WHILE construct.
You can nest WHERE statements within a WHERE construct. A where_assignment_statement that is a defined assignment must be an elemental defined assignment.
In each where_assignment_statement, the mask_expr and the variable being defined must be arrays of the same shape. Each mask_expr in a WHERE construct must have the same shape.
A WHERE statement that is part of a where_body_construct must not be a branch target statement.
The execution of a function reference in the mask_expr of a WHERE statement can affect entities in the where_assignment_statement.
See Interpreting masked array assignments for information on interpreting mask expressions.
If a where_construct_name appears on a WHERE construct statement, it must also appear on the corresponding END WHERE statement. A construct name is optional on any masked ELSEWHERE and ELSEWHERE statements in the WHERE construct.
A where_construct_name can only appear on a WHERE construct statement.
REAL, DIMENSION(10) :: A,B,C ! In the following WHERE statement, the LOG of an element of A ! is assigned to the corresponding element of B only if that ! element of A is a positive value. WHERE (A>0.0) B = LOG(A)
·
·
·
END
The following example shows an elemental defined assignment in a WHERE statement:
INTERFACE ASSIGNMENT(=) ELEMENTAL SUBROUTINE MY_ASSIGNMENT(X, Y) LOGICAL, INTENT(OUT) :: X REAL, INTENT(IN) :: Y END SUBROUTINE MY_ASSIGNMENT END INTERFACE INTEGER A(10) REAL C(10) LOGICAL L_ARR(10) C = (/ -10., 15.2, 25.5, -37.8, 274.8, 1.1, -37.8, -36.2, 140.1, 127.4 /) A = (/ 1, 2, 7, 8, 3, 4, 9, 10, 5, 6 /) L_ARR = .FALSE. WHERE (A < 5) L_ARR = C ! DATA IN ARRAY L_ARR AT THIS POINT: ! ! L_ARR = F, T, F, F, T, T, F, F, F, F END ELEMENTAL SUBROUTINE MY_ASSIGNMENT(X, Y) LOGICAL, INTENT(OUT) :: X REAL, INTENT(IN) :: Y IF (Y < 0.0) THEN X = .FALSE. ELSE X = .TRUE. ENDIF END SUBROUTINE MY_ASSIGNMENT