015. merge(x, y, cond)#

topic: Conditionals

merge(x, y, cond) returns x if the condition cond is true, otherwise y. We can get the same result as the previous tip:

merge.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
integer :: i

do i = -1, 1
   print *, i
   ! Note: "zero" padded to have same length as "negative"
   print *, merge("negative", merge("zero    ", &  
                  "positive", i == 0 ), i < 0)
end do

end
Output1#
          -1
 negative
           0
 zero    
           1
 positive

Similar to the three-argument usage of numpy.where.



1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags