| United States-English |
|
|
|
![]() |
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 5 Expressions and assignmentAssignment |
|
An assignment operation defines a variable by giving it a value. In HP Fortran 90, there are four types of assignment:
The following sections describe the first three assignment types. The last—defined assignment—is defined by the programmer, using the INTERFACE statement. For information about defined assignment, see “Defined assignment”. An assignment statement gives the value of an expression to a variable. It has the following syntax: variable = expression variable may be any nonpointer variable or a pointer variable that is associated with a target. (If variable is a pointer, expression is assigned to the target.) The valid combinations of types for variable and expression are given in Table 5-5 “Conversion of variable=expression”. The intrinsic functions that document the conversions are described in Chapter 11. Table 5-5 Conversion of variable=expression
As described in “Bitwise operators”, 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. As shown in 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 variable is a scalar, expression must be scalar. If variable is an array or an array section, expression must be either an array-valued expression of the same shape or a scalar. If variable is an array or an array section, and expression is a scalar, the value of expression is assigned to all elements of variable. If variable and expression are both arrays, the assignment is carried out element by element with no implied ordering. The expression is evaluated completely before the assignment is started. For example, the following code segment:
sets c(2:4) to "abc", not to "aaa", which might result from a left-to-right character-by-character assignment. The following examples illustrate assignments of different data types:
Pointer assignment establishes an association between a pointer and a target. Once the association is established, if the pointer is referenced on the left-hand side of an assignment statement, it is the target to which the assignment is made. And if the pointer is referenced in an expression, the target is taken as the operand in the expression. The syntax of a pointer assignment is: pointer-object => target-expression
The type, kind, and rank of pointer-object and target-expression must be the same. If target-expression is an array, it cannot be an assumed-size array or an array section with a vector subscript. For information about assumed-size arrays, see “Assumed-size arrays”. For information about array sections with vector subscripts, see “Vector subscripts”. If target-expression is a pointer already associated with a target, then pointer-object becomes associated with the target of target-expression. If target-expression is a pointer that is disassociated or undefined, then pointer-object inherits the disassociated or undefined status of target-expression. For information about pointer status, see “Pointer association status”. The following example, ptr_assign.f90, illustrates association of scalar and array pointers with scalar and array targets: Example 5-1 ptr_assign.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
In a masked array assignment, a logical expression—called a mask— controls the selection of array elements for assignment. Masked array assignment is implemented by the WHERE statement and the WHERE construct. The syntax of the WHERE statement is: WHERE (array-logical-expression) array = array-expression where array-logical-expression, array, and array-expression must all be conformable. The array-logical-expression (the mask) is evaluated for each element and the outcome (.TRUE. or .FALSE.) determines whether an assignment is made to the corresponding element of array. The syntax of the WHERE construct is:
The WHERE construct is similar to the WHERE statement, but more general in that several array = array-expression statements can be controlled by one array-logical-expression. In addition, an optional ELSEWHERE part of the construct assigns array elements whose corresponding array-logical-expression elements evaluate to .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 in a WHERE statement, as follows: WHERE (array-logical-expression) array = array-expression Each assignment in the ELSEWHERE is executed as if it were: WHERE (.NOT.array-logical-expression) array = array-expression For example, the following WHERE construct:
is evaluated as if it was specified as:
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 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 in the ELSEWHERE block. Notice, however, that results generated in an ELSEWHERE block cannot feed back into variables in the WHERE block. The following example score2grade.f90 illustrates the use of a masked assignment to find the letter-grade equivalent for each test score in the array test_score. To do the same operation without the benefit of masked array assignment would require a DO loop iterating over the array either in an IF-ELSE-IF construct or in a CASE construct, testing and assigning to each element at a time. Example 5-2 score2grade.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
The next example is a subroutine that uses the WHERE construct to replace each positive element of array a 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. In the ELSEWHERE part of the construct, the assignment to array a should not appear before the assignment to array ca; otherwise, all of ca will be set to zero.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||