Suppose for example we have three real matrices:
The form of the construct is
where (C /= 0) A = B/C end whereThe argument of WHERE is evaluated first. This argument is a matrix of logical elements, and is called the mask. The mask must conform to the implied shape of each assignment in the body. In our example
Next, the statements in the body of the WHERE construct are evaluated
succesively. The matrix assignment C = A/B is performed element
by element, but only for those elements for which
.TRUE.; in our example, the result of the masked assignment is
The complete form of the WHERE construct includes an ELSEWHERE branch as well:
where (C /= 0) A = B/C elsewhere A = -1.0 end whereThe ELSEWHERE assignments are performed for those elements for which .FALSE.; the result of our example is
Note: a short form is possible; similar to the IF statement, there is a WHERE statement which reads
where (C /= 0) A = B/C
WHERE statements cannot be nested. The execution first evaluates the mask (a matrix of .TRUE./.FALSE. depending on the elemental value of the condition) then executes the assignments for the .TRUE. positions (the WHERE block), then execute the assignments for the .FALSE. positions (the ELSEWHERE block).