3

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 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Firstly, thank you. So just to make sure I understand, what it's doing is making an array using (50, i) for i=1 and 2, resulting in (50, 1, 50, 2), and if I wanted to have the 50 separate to the implied do, I'd do a = [50, (i,i=1,3)]?
Correct on both parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.