Implied do construct repeating scalar values in an array constructor

Viewed 81

I'm trying to figure out why the placement of a scalar before a do construct inside an array constructor produces the answer it does.

I understand the do construct in general, that (/(i,i=1,5)/) is equivalent to (/1,2,3,4,5/).

real, dimension(4) :: a
integer :: i
a=(/(50,i,i=1,2)/)

This produces the array 50.0000000, 1.00000000, 50.0000000, 2.00000000. At best I would expect an error because the step size doesn't produce enough values to fill the array.

1 Answers

The array produced with multiple 50s is the expected one.

Part of the question text does not match what happens with the code:

the placement of a scalar before a do construct inside an array constructor

In [(50,i,i=1,2)] (using Fortran 2003 [...] for clarity) the scalar value 50 is not "before" the implied do but is a clear part of it. This can be compared with

a = [50, (i,i=1,3)]

which would result in the constructed array [50, 1, 2, 3].

The formal syntax for an array constructor's implied do is

( ac-value-list , ac-implied-do-control )

so it can be seen that multiple values can be part of the range of the implied do control even though it is common for a single value only to be used. With such multiple values they are all evaluated for each iteration of the implied do (and 50 evaluates the same each time) and used in the array construction.

Related