hbaromega
hbaromega

Reputation: 2374

Same value assignment of multiple variables in a single statement

Is there a way where I can assign the same value for different variables without constructing an array in a single statement?

For example, if I have variables a,b,c,d, and e, can I assign something like

a=b=c=d=e=10.0 

?

I know that I can do in a single line:

a=10.0; b=10.0; c=10.0; d=10.0; e=10.0

But that is not I want since if I want to change the value 10.0 later to something else, I have to make the change everywhere.

Upvotes: 3

Views: 5975

Answers (4)

Bálint Aradi
Bálint Aradi

Reputation: 3812

You probably should consider to use an array instead of individual variables, especially if they serve similar purposes:

real :: myarray(5)

myarray(:) = 10.0

Upvotes: 3

High Performance Mark
High Performance Mark

Reputation: 78316

Come on Fortranners, you know you want to ...

equivalence(a,b,c,d,e)

Now all those rascals are going to have the same value at all times.

Upvotes: 4

M. S. B.
M. S. B.

Reputation: 29391

Perhaps:

real, parameter :: NamedConst = 10.0

a=NamedConst; b=NamedConst; c=NamedConst; d=NamedConst; e=NamedConst

Then if you should use the special value NamedConst in more than one line, there is clearly a single place to change its value.

Upvotes: 4

Alexander Vogt
Alexander Vogt

Reputation: 18098

The first version is not possible in Fortran. Following the (2008) Standard, an assignment is of the general form (ch. 7.2.1.1)

variable = expr

But why don't you try something like:

a=10.0; b=a; c=a; d=a; e=a

That way, you just need to change the value of a later on!

Upvotes: 6

Related Questions