Lahey/Fujitsu Fortran

Assignment Statement

Description

The assignment statement assigns the value of the expression on the right side of an equal sign to the variable on the left side of the equal sign.

Syntax

variable=expression

Where:

variable is a scalar variable, an array, or a variable of derived type.
expression is an expression whose result is conformable with variable.

Remarks

A numeric variable can only be assigned a numeric value; a CHARACTER variable can only be assigned a CHARACTER value of the same kind; a LOGICAL variable can only be assigned a LOGICAL value; a derived type variable can only be assigned a value of the same derived type.

Evaluation of expression takes place before the assignment is made.

If the kind of expression is different from that of variable, the result of expression undergoes an implicit type conversion to the kind and type of variable, possibly causing a loss of precision.

If expression is an array, then variable must be an array. If expression is scalar and variable is an array, all elements of variable are assigned the value of expression.

If variable is a pointer, it must be associated with a target. The target is assigned the value of expression.

If variable and expression are of CHARACTER type with different lengths, expression is truncated if longer than variable, and padded on the right with blanks if expression is shorter than variable.

Example 1

! Basic assignment examples integer :: i1,i2 real :: r1,r2 real(kind(1.d0)) :: d1,d2 complex :: q1,q2 logical :: l1,l2 character(len=6) :: c1,c2 ! assignment to a constant i1=12345 r1=12345. d1=12345.d0 q1=cmplx(1.2345e0,-1.2345e0) l1=.true. c1="Howdy!" write(*,*) i1,r1,d1,q1,l1,c1 ! assignment to an expression i2=i1/10 r2=r1*10. d2=d1**10.d0 q2=q1/r1 l2=(r1 < d1) c2=c1(1:1) // "ow" // c2(4:6) write(*,*) i2,r2,d2,q2,l2,c2

Example 2

! Conversion examples integer(kind=selected_int_kind(4)) :: ishort=12345 integer(kind=selected_int_kind(6)) :: ilong real(kind(1.e0)) :: a=1.234567e6, b1, b2 real(kind(1.d0)) :: d1, d2 ! safe conversions ilong=ishort b1=ishort b2=ilong d1=ishort write(*,*) ishort,ilong,b1,b2,d1 ! dangerous conversions ilong=a ! succeeds this time ishort=ilong ! overflows write(*,*) a,ishort,ilong ishort=a ! overflows write(*,*) ishort,a ilong=a*b1 ! overflows write(*,*) ilong,a*b1 ! loss of precision d1=exp(1.5d0) ! no loss of precision b1=d1 ! loses precision d2=b1 ! d2 given a single precision value write(*,'(4(/,g21.14))') exp(1.5d0),d1,d2,b1 b2=huge(ilong) ! loses precision write(*,*) b2,huge(ilong)

Example 3

! array and derived type assignments real :: a=0. real,dimension(3) :: a_a1=0. real,dimension(3,3) :: a_a2=0. type realtype1 ! derived type definition real :: a=0. real,dimension(3) :: a_a1=0. real,dimension(3,3) :: a_a2=0. end type realtype1 type realtype2 ! derived type definition real :: a=0. real,dimension(3) :: a_a1=0. real,dimension(3,3) :: a_a2=0. end type realtype2 type (realtype1) :: rt1 ! derived type declaration type (realtype2) :: rt2 ! derived type declaration ! array assignment to a scalar constant a_a1=13. a_a2=16. rt1%a_a2=19. write(*,10) a_a1, a_a2, rt1%a_a2 ! Array assignment to a scalar variable a_a1=a a_a2=a rt1%a_a2=a write(*,10) a_a1,a_a2,rt1%a_a2 ! Array assignment to an array constant a_a1=(/1.,2.,3./) a_a2=reshape((/1.,2.,3.,4.,5.,6.,7.,8.,9./),(/3,3/)) rt1%a_a2=reshape((/9.,8.,7.,6.,5.,4.,3.,2.,1./),(/3,3/)) write(*,10) a_a1,a_a2,rt1%a_a2 ! Array assignment to a derived type constant rt2=realtype2 (0.,(/1.,2.,3./),& reshape((/1.,2.,3.,4.,5.,6.,7.,8.,9./),(/3,3/))) write(*,20) rt2%a, & rt2%a_a1,rt2%a_a2 ! Conformable assignments rt2%a=sum(a_a1) rt1%a_a1=a_a1 rt2%a_a1=rt1%a_a1 rt2%a_a2(3,:)=a_a1 rt2%a_a2(1:2,1:2)=rt1%a_a2(1:2,2:3) write(*,20) rt2%a, rt2%a_a1, rt2%a_a2 10 format(/,3f7.3,2(/,3(/,3f7.3)),/) 20 format(/,f7.3,//,3f7.3,/,3(/,3f7.3),/) ! non-conformable assignments ! will produce error messages ! a=a_a2 ! rt1=0. ! rt1=a ! rt1=rt2 ! rt1%a_a1=a_a2 ! rt1%a_a2=a_a1