Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran 90 Programmer's Reference: HP Series 700/800 Computers > Chapter 5 Expressions and assignment

Assignment

» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

The following sections describe the assignment statement, pointer assignment, and masked-array assignment (the WHERE construct).

Assignment statement

An assignment statement transfers the value of an expression to a variable.

The syntax of an assignment statement is:

variable = expression

The interpretation of the assignment is defined for the allowed intrinsic type combinations of variables and expressions; these are intrinsic assignments. Assignments for additional combinations can be defined by inclusion of the appropriate defined assignment interfaces and corresponding subroutine subprograms, as detailed in Chapter 7.

Intrinsic assignment

The variable may be any nonpointer variable or a pointer variable that is associated with a target.

The valid combinations of types for the variable and the expression are given in the following table. The intrinsic functions used to describe the conversions are detailed in Chapter 11.

Table 5-5 Conversion of variable=expression

Variable type

Expression type

Conversion

integer

integer, real, or complex

INT(expression, KIND(variable))

real

integer, real, or complex

REAL(expression, KIND(variable))

character

character (same kind parameters)

CMPLX(expression, KIND(variable))

logical

logical

Truncate expression if expression length is greater than variable length; otherwise, pad value assigned to variable, with blanks if necessary.

logical

logical

LOGICAL(expression, KIND(variable))

derived type

same derived type

None

 

As described in the section “Interpretation of expressions”, HP Fortran 90 allows integer and logical operands to be used interchangeably. HP Fortran 90 also allows logical expressions to be assigned to integer variables and integer expressions to logical variables. So, from Table 5-5 “Conversion of variable=expression, a logical expression may also be assigned to real or complex variables, and similarly, a real or complex expression may be assigned to a logical variable.

If the variable is a scalar, the expression must be scalar. If the variable is an array or an array section, the expression must be an array valued expression of the same shape or a scalar. If the variable is an array or an array section, and the expression is a scalar , the value of the expression is assigned to all elements of the variable. If the variable and expression are arrays, the assignment is carried out element by element with no ordering implied.

The expression is evaluated completely before the assignment is started. For example:

character (len=4):: c
c(1:4) = 'abcd'
c(2:4) = c(1:3)  

sets c(2:4) to "abc", not to "aaa", which might result from a left-to-right character-by-character assignment.

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

The following illustrates asignments of different data types:

integer icnt
type circle
   real radius
   real xreal y
end type
type (circle) circle1, circle2
real area, pi
logical boolx, booly, pixel(10,10)
integer a(10,5)
integer, dimension (10,10):: matrix1, matrix2
character*3 initials
character*10 surname
character*20 name
icnt = icnt + 1    !integer assignment
circle1 = circle2  ! derived-type assignment
area = pi * circle%radius**2  ! real assignment
pixel(x,y) = boolx .AND. booly
!assigns a logical expression to an element of
!the logical array pixel
a(:,1:2) = 0
!first two columns of a are set to zero
maxtrix1 = maxtrix2
!each element of maxtrix2 is assigned to the
!corresponding element of maxtrix1
name = initials // surname
!example of a character assignment

Pointer assignment

The pointer assignment statement establishes an association between a pointer object and a target.

The syntax is:

pointer-object=> target

subject to the following constraints:

  • pointer-object is a variable or variable component with the POINTER attribute; if target is a variable, it must have the TARGET or POINTER attribute. If target is an expression, then it must either be a reference to a function that returns a pointer result or a user-defined operation that returns a pointer result.

  • The type, kind parameters and rank of pointer-object and target must be the same.

  • target cannot be an assumed-size whole array or an array section with a vector subscript.

If target is a pointer already associated with a target, then pointer-object becomes associated with the target of target. If target is a pointer that is disassociated or undefined, then pointer-object inherits the disassociated or undefined status of target.

The following example illustrates association of scalar and array pointers with scalar and array targets:

integer, pointer :: p1, p2, p3(:)
integer, target :: t1, t2(10)
! p1, p2 and p3 are currently undefined.
p1 => t1       ! p1 is associated with t1.
p2 => p1       ! p2 is associated with t1.
               ! p1 remains associated with t1.
p1 => t2(1)    ! p1 is associated with t2(1).
               ! p2 remains associated with t1.
p3 => t2       ! p3 is associated with t2.
p1 => p3(2)    ! p1 is associated with t2(2).
NULLIFY(p1)    ! p1 is disassociated.
p2 => p1       ! Now p2 is also disassociated.

Masked array assignment

In masked array assignment, a logical array expression, the mask, controls evaluation of the array expressions and assignment to the array variables.

Masked array assignment is provided in Fortran 90 by the WHERE statement and the WHERE construct.

The syntax of the WHERE statement is:

WHERE (array-logical-expression) array = expression

array-logical-expression, array, and expression must all be conformable. The array-logical-expression (the mask) is evaluated for each element and the outcome (.TRUE. or .FALSE.) used to determine whether or not an assignment is made to the corresponding element of array.

The syntax of the WHERE construct is:

WHERE ( array-logical-expression )
       array = expression
       [array = expression] ...
[ELSEWHERE
       array = expression
       [array = expression] ... ]
END WHERE

The WHERE construct is similar to the WHERE statement, but more general in that several array = expression clauses can be controlled by one array-logical-expression. In addition, an optional ELSEWHERE part of the construct may be used to assign array elements whose corresponding array-logical-expression elements evaluate .FALSE..

When a WHERE construct is executed, array-logical-expression is evaluated just once and therefore any subsequent assignment in a WHERE block (the block following the WHERE statement), or ELSEWHERE block to an entity of array-logical-expression has no effect on the masking. Thereafter, successive assignments in the WHERE block are evaluated in sequence as if they were specified as:

WHERE (array-logical-expression) array = expression

Each assignment in the ELSEWHERE is executed as if it were:

WHERE (.NOT.array-logical-expression) array  = expression

For example, the following WHERE construct:

WHERE (a > b)
  a = b
  b = 0
ELSEWHERE
  b = a
  a = 0
END WHERE

is evaluated as if it was specified as:

mask = a > b
WHERE (mask) a = b
WHERE (mask) b = 0
WHERE (.NOT.mask) b = a
WHERE (.NOT.mask) a = 0

Only assignment statements may appear in a WHERE block or an ELSEWHERE block. Within a WHERE construct, only the WHERE statement may be the target of a branch.

The following are examples of mask array assignment:

REAL, dimension(5) :: a
WHERE (a > 0.0) a = SQRT(a)

Each positive element of array a is replaced by its square root.

REAL, DIMENSION(5) :: a
COMPLEX, DIMENSION(5) :: ca
WHERE (a > 0.0)
   ca = CMPLX(0.0)
   a  = SQRT(a)
ELSEWHERE
   ca = SQRT(CMPLX(a))
   a  = 0.0
END WHERE

Each positive element of array a is replaced by its square root; the remaining elements calculate the complex square roots of their values, which are then stored in the corresponding elements of the complex array ca. Note that in the ELSEWHERE clause the assignment to array a should not appear before the assignment to array ca; otherwise, all of ca will be set to zero.

The form of a WHERE construct is similar to that of an IF construct, but with this important difference: no more than one block of an IF construct may be executed, but in a WHERE construct at least one (and possibly both) of the WHERE and ELSEWHERE blocks will be executed. In a WHERE construct, this difference has the effect that results in a WHERE block may feed into, and hence affect, variables used in the ELSEWHERE block. Notice, however, that results generated in an ELSEWHERE block cannot feed back into variables used in the WHERE block.

The following demonstrates how results in a WHERE block could affect assignments in the ELSEWHERE block:

REAL, DIMENSION x(100)
WHERE (x(2:100) /= 0.0)
  x(2:100) = 1.0/x(2:100)  !the last 99
                           !non-zero elements
                           !of x are replaced
                           !by their reciprocal
ELSEWHERE
  x(2:100) = x(1:99)       !the last 99 zero
                           !elements of x are
                           !replaced by their
                           !preceding neighbor
                           !which may have
                           !been modified by
                           !the WHERE block
END WHERE
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1996 Hewlett-Packard Development Company, L.P.