Same value assignment of multiple variables in a single statement

时光总嘲笑我的痴心妄想 提交于 2020-08-07 07:12:07

问题


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.


回答1:


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.




回答2:


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!




回答3:


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.




回答4:


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

real :: myarray(5)

myarray(:) = 10.0


来源:https://stackoverflow.com/questions/23654753/same-value-assignment-of-multiple-variables-in-a-single-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!