Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 916
Alessandro B Avatar asked Aug 15 '13 16:08

Alessandro B


1 Answers

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
like image 159
Kyle Kanos Avatar answered Oct 02 '22 02:10

Kyle Kanos