| United States-English |
|
|
|
![]() |
HP Fortran 90 Programmer's Reference: HP Fortran 90 Programmer's Reference > Chapter 5 Expressions and assignmentExpressions |
|
An expression is the specification of data and, possibly, a set of operations that enable the computer to evaluate the expression and produce a value. Because an expression results in a value, it has a type, kind, and shape. If an expression is of the character type, it also has a length parameter. The general form of an expression is: [operand1] operator operand2
The following sections describe operands, operators, and expressions in more detail. An operand may be any of the following:
Any variable or function reference used as an operand in an expression must have been previously defined. Likewise, any pointer must have been previously associated with a target. If an operand has the POINTER attribute, the target associated with it is the operand. When an operand is a whole array reference, the complete array is referenced. An assumed-size array variable cannot be an operand. An array section of an assumed-size array can be an operand if the extent of the last dimension of the section is defined by the use of a subscript, a section subscript with an extent for the upper bound, or a vector subscript. (Assumed-size arrays are discussed in “Assumed-size arrays”, and array sections in “Array sections”.) If two operands in an expression are arrays, they must have the same shape. If one operand is a scalar, it is treated as if it were an array of the same shape as the other operand, in which all elements have the value of the scalar. The result of the operation is an array in which each element is the result of applying the operator repeatedly to corresponding elements of the two operands. The rules governing how the use of operands in an expression vary, depending on the type of expression. For example, some operands that may appear on the right-hand side of an assignment statement but not in an initialization expression. See “Special forms of expression” for detailed information about the different forms of an expression and the restrictions that those forms impose on operands. HP Fortran 90 recognizes the following types of operators:
All of these except the last are intrinsic operators—that is, the operations they perform are defined by HP Fortran 90. Intrinsic operators are described in the following sections. Defined operators are those that the programmer defines—or overloads, if the operator already has already been defined—using the INTERFACE statement. Defined operators and overloading are discussed in “Defined operators”. The arithmetic operators are:
Two operands joined by a binary operator can be of different numeric types or different kind type parameters. The type of the result is:
Except for a value raised to an integer power, each operand that differs in type or kind type parameter from that of the result is converted to a value with the type and kind type of the result before the operation is performed. Logical and integer operands can be combined with arithmetic operators. The logical operand is treated as an integer of the same kind type parameter, and the result of the operation is of type integer. If the operands have different kind type parameters, the shorter is considered to be extended as a signed integer. For information about logical values, see “Logical operators”. The arithmetic operators behave as expected, with the following qualifications:
The following are HP extensions to Standard:
The relational operators are .EQ., .NE., .GT., .GE., .LT., .LE., ==, /=, >, >=, <, and <=. All relational operators are binary. The letter forms of the relational operators have the same meaning as the symbol forms. Thus, .EQ. is a synonym for ==, .NE. is a synonym for /=, and so on. If the operands in a relational operation are numerical expressions with different type or kind type parameters, the operands are converted to the type and kind type parameters that the sum of the operands would have and are then compared; see “Arithmetic operators” for information about the result of mixed arithmetic expressions. If the operands are character expressions, the shorter operand is blank-padded to the length of the other prior to the comparison. The comparison starts at the first character and proceeds until a character differs or equality is confirmed. See Appendix C for the collating sequence. The concatenation operator is //. It is binary. In a concatenation operation, each operand of the concatenation operator must be of type character and have the same kind type parameter. The character length parameter of the result is the sum of the character length parameters of the operands. The logical operator are .AND., .OR., .EQV., .NEQV., .XOR., and .NOT.. The .NOT. operator is unary; the others are binary. The .XOR. is an HP extension having the same meaning as the .NEQV. operator. As an HP extension, the operands of a logical expression may be of type integer. Functions returning integers may appear in logical expressions, and functions returning logicals may appear in integer expressions. If the operands of a logical operation have different kind type parameters, the operand with the smaller parameter is converted to a value with the larger parameter before the operation is performed. The result has the larger kind type parameter. Table 5-1 “Logical operators” shows the behavior of the logical operators for the different permutations of operand values. Note that the .XOR. operator is a synonym for the .NEQV. operator and behaves similarly. Table 5-1 Logical operators
As an extension to the Standard, HP Fortran 90 allows logical operators to be used as bitwise operators on integer operands. The logical operations are bitwise; that is, they are performed for each bit of the binary representations of the integers. When the operands are of different lengths, the shorter is considered to be extended to the length of the other operand as if it were a signed integer, and the result has the length of the longer operand. When logical operators are used on integer operands, any nonzero value is considered .TRUE., and a zero value is considered .FALSE. . In general, an actual argument of type integer may not be used in a reference to a procedure when the corresponding dummy argument is of type logical, nor may an actual argument of type logical be used when the dummy argument is of type integer. As an HP extension, logical and integer arguments may be used interchangeably in calls to bit manipulation intrinsics. See Chapter 11 “Intrinsic procedures” for information about the bit manipulation intrinsics. The following example shows the use of the .AND. operator to perform a bitwise AND operation:
The next example shows the use of logical operators to perform bit-masking operations.
The next example makes a standard-conforming reference to a bit manipulation intrinsic:
HP Fortran 90 allows the following nonstandard version of the preceding example:
When an expression expands to operand1 operator1 operand2 operator2 operand3 ... each operator is assigned a precedence. The defined order of evaluation is that any subexpressions containing an operator with higher precedence than the adjacent operators is evaluated first. Where operators are of equal precedence, evaluation is from left to right. The exception to this rule is the exponentiation operator (**), which is evaluated from right to left. Any expression or subexpression may be enclosed in parentheses. These expressions are always evaluated first, using the rules explained above. This usage of parentheses is therefore equivalent to normal mathematical usage. Table 5-2 “Operator precedence” lists the precedence of the operators, and Table 5-3 “Examples of operator precedence” gives example expressions that illustrate operator precedence. Table 5-2 Operator precedence
Table 5-3 Examples of operator precedence
The Standard allows the compiler to generate code that evaluates an expression by any sequence that produces a result mathematically equivalent to the sequence implied by the statement. This laxity permits code optimization, including (for example) the reordering of expressions and the promotion of common subexpressions. Because the order of evaluation is not defined by the Standard, a function reference within an expression may not modify any of the other operands within the same expression. For example, fun(x)+x is indeterminate if the reference to fun modifies the value of the argument x. Certain language constructs allow only restricted forms of expressions. For example, the value specified for a named constant in a PARAMETER statement may be defined by an expression, but it must be possible to evaluate the expression at compile-time. This means that the expression must not contain any operands that depend on program execution for their value. To take another example, a bound of a dummy array argument may be specified as an expression, but it must be possible to evaluate this expression on entry to the subprogram. There are special restrictions imposed on operands and operators that may appear in an expression, depending on whether the expression is one of the following:
The following sections describe the special forms of expression. A constant expression is either a constant or an expression containing only intrinsic operators and constant operands. This restriction also applies to any clearly defined part of a constant—for example, a substring with constant start and end points, or an array or structure constructor. A constant expression may include references to intrinsic functions that can be evaluated at compile-time. A constant expression may appear in any context in which any expression may appear. The following are examples of constant expressions:
An initialization expression is a more specialized form of constant expression that can appear as the initial value in a declaration statement. Initialization expressions have these additional restrictions:
Initialization expressions are required for the following:
The following entities may not be initialized:
The following are examples of initialization expressions:
The following are illegal initialization expressions:
For information about initializing arrays with an array constructor, see “Array constructors”. A specification expression has a scalar value, is of type integer, and can be evaluated on entry to the scoping unit in which it appears. A specification expression may appear (for example) as a bound in an array declaration or as the length in a CHARACTER type declaration. An operand in a specification expression is one of the following:
The differences between specification expressions and initialization expressions are summarized in Table 5-4 “Initialization and specification expressions”. Table 5-4 Initialization and specification expressions
The following are examples of specification expressions:
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||