Alessandro B
Alessandro B

Reputation: 111

Equivalence of IF and WHERE

We all know that the DO loop is more powerful than the FORALL statement in Fortran. That is, you can always substitute a FORALL by a DO, but not vice versa.

What about the WHERE statement and block?

Can I always substitute the IF by a WHERE? Is it always possible to code the conditionals and bifurcations with a WHERE, thus avoiding the IF?

Upvotes: 5

Views: 1043

Answers (1)

Kyle Kanos
Kyle Kanos

Reputation: 3264

WHERE statements are reserved for arrays assignments and nothing else, e.g.:

INTEGER, DIMENSION(100,100) :: a, b
... define a ...
WHERE(a < 0)
   b = 1
ELSEWHERE
   b = 0
ENDWHERE

If you tried adding in something, say a WRITE statement, inside the WHERE block, you would see something like the following compiling error (compiler dependent):

Error: Unexpected WRITE statement in WHERE block at (1)

EDIT

Note that nested WHERE blocks are legal:

WHERE(a < 0)
   WHERE( ABS(a) > 2)
      b = 2
   ELSEWHERE
      b = 1
   ENDWHERE
ELSEWHERE
   b = 0
ENDWHERE

Upvotes: 10

Related Questions