Assignment statements are executable statements that define or redefine variables based on the result of expression evaluation.
A defined assignment is not intrinsic, and is defined by a subroutine and an interface block. See Defined Assignment.
The general form of an intrinsic assignment is:
>>-variable-- = --expression----------------------------------->< |
The shapes of variable and expression must
conform. variable must be an array if expression is
an array (see Expressions Involving Arrays). If expression is a scalar and
variable is an array, expression is treated as an array of
the same shape as variable, with every array element having the same
value as the scalar value of expression. variable
must not be a many-one array section (see Vector Subscripts for details), and neither variable nor
expression can be an assumed-size array. The types of
variable and expression must conform as follows:
Type of variable | Type of expression |
---|---|
Numeric | Numeric |
Logical | Logical |
Character | Character |
Derived type | Derived type (same as variable) |
In numeric assignment statements, variable and expression can specify different numeric types and different kind type parameters. For logical assignment statements, the kind type parameters can differ. For character assignment statements, the length type parameters can differ.
If the length of a character variable is greater than the length of a character expression, the character expression is extended on the right with blanks until the lengths are equal. If the length of the character variable is less than the character expression, the character expression is truncated on the right to match the length of the character variable.
If variable is a pointer, it must be associated with a definable target that has type, type parameters and shape that conform with those of expression. The value of expression is then assigned to the target associated with variable.
Both variable and expression can contain references to any portion of variable.
An assignment statement causes the evaluation of expression and all expressions within variable before assignment, the possible conversion of expression to the type and type parameters of variable, and the definition of variable with the resulting value. No value is assigned to variable if it is a zero-length character object or a zero-sized array.
A derived-type assignment statement is an intrinsic assignment statement if there is no accessible defined assignment for objects of this derived type. The derived type expression must be of the same derived type as the variable. (See Determining Type for Derived Types for the rules that determine when two structures are of the same derived type.) Assignment is performed as if each component of the expression (or each pointer) is assigned to the corresponding component of the variable. Pointer assignment is executed for pointer components and intrinsic assignment is performed for nonpointer nonallocatable components. For an allocatable component the following sequence of operations is applied:
The value of the component of expression is then assigned to the corresponding component of variable using defined assignment if there is a defined assignment accessible for the objects, otherwise intrinsic assignment is used.
When variable is a subobject, the assignment does not affect the definition status or value of other parts of the object.
For numeric intrinsic assignment, the value of expression may be
converted to the type and kind type parameter of variable, as
specified in the following table:
Type of variable | Value Assigned |
---|---|
Integer | INT(expression,KIND=KIND(variable)) |
Real | REAL(expression,KIND=KIND(variable)) |
Complex | CMPLX(expression,KIND=KIND(variable)) |
+-------------------------------IBM Extension--------------------------------+
+----------------------------End of IBM Extension----------------------------+
Only as much of the character expression as is necessary to define the character variable needs to be evaluated. For example:
CHARACTER SCOTT*4, DICK*8 SCOTT = DICK
This assignment of DICK to SCOTT requires only that you have previously defined the substring DICK(1:4). You do not have to previously define the rest of DICK (DICK(5:8)).
+-------------------------------IBM Extension--------------------------------+
If expression is of type arithmetic, arithmetic assignment is used. Similarly, if expression is of type character, character assignment is used, and if expression is of type logical, logical assignment is used. If the expression on the right is of type BYTE, arithmetic assignment is used.
+----------------------------End of IBM Extension----------------------------+
INTEGER I(10) LOGICAL INSIDE REAL R,RMIN,RMAX REAL :: A=2.3,B=4.5,C=6.7 TYPE PERSON INTEGER(4) P_AGE CHARACTER(20) P_NAME END TYPE TYPE (PERSON) EMP1, EMP2 CHARACTER(10) :: CH = 'ABCDEFGHIJ' I = 5 ! All elements of I assigned value of 5 RMIN = 28.5 ; RMAX = 29.5 R = (-B + SQRT(B**2 - 4.0*A*C))/(2.0*A) INSIDE = (R .GE. RMIN) .AND. (R .LE. RMAX) CH(2:4) = CH(3:5) ! CH is now 'ACDEEFGHIJ' EMP1 = PERSON(45, 'Frank Jones') EMP2 = EMP1 ! EMP2%P_AGE is assigned EMP1%P_AGE using arithmetic assignment ! EMP2%P_NAME is assigned EMP1%P_NAME using character assignment END