6

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?

milancurcic
  • 5,964
  • 2
  • 31
  • 45
Alessandro B
  • 121
  • 3
  • 3
    The original intent of FORALL was to allow the masked array assignments to be done in parallel - it came from a variant called High Performance Fortran and was adopted into Fortran 95. Unfortunately, the semantics of FORALL were not conducive to parallelization, so Fortran 2008 added DO CONCURRENT which is not only more familiar-looking to Fortran programmers but also has better semantics for parallelization. You may want to look at MERGE, which can, with some clever masks, be quite powerful. – Steve Lionel Aug 15 '13 at 19:09
  • Going on what @SteveLionel said, note that `MERGE` *can* be used inside the `WHERE` block while an `IF` statement *cannot*. – Kyle Kanos Aug 16 '13 at 00:53
  • 1
    Also keep in mind that `FORALL` is purely an assignment statement or block, while `DO` is a more general flow-control construct. – milancurcic Aug 16 '13 at 12:25

1 Answers1

9

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
Kyle Kanos
  • 3,129
  • 1
  • 21
  • 37