HP 3000 Manuals

Assignment Statement (Executable) [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Reference

Assignment Statement (Executable) 

The assignment statement evaluates an expression and assigns the
resulting value to a data item.  There are four kinds of assignment
statements:

   *   Arithmetic
   *   Logical
   *   Character
   *   Aggregate

	       Click here to view figure.
            

----------------------------------------------------------------------------------------------
|                              |                              |                              |
|             Item             |     Description/Default      |         Restrictions         |
|                              |                              |                              |
----------------------------------------------------------------------------------------------
|                              |                              |                              |
| variable1                    | Variable or array element.   | If arithmetic assignment,    |
| array_element                |                              | type must be integer, real,  |
| scalar_record_field_name     |                              | or complex.                  |
| substring                    |                              |                              |
|                              |                              | If logical assignment, type  |
|                              |                              | must be logical.             |
|                              |                              |                              |
|                              |                              | If character assignment,     |
|                              |                              | type must be character.      |
|                              |                              |                              |
----------------------------------------------------------------------------------------------
|                              |                              |                              |
| expression                   | The expression to which the  | If the variable or array     |
|                              | variable or array element is | element is arithmetic,       |
|                              | being assigned.              | expression must be an        |
|                              |                              | arithmetic expression; if    |
|                              |                              | logical, expression must of  |
|                              |                              | type logical; if character,  |
|                              |                              | expression must be of type   |
|                              |                              | character.                   |
|                              |                              |                              |
----------------------------------------------------------------------------------------------
|                              |                              |                              |
| aggregate1                   | A record with one or more    | Both must be declared with   |
| aggregate2                   | fields.                      | the same structure.          |
|                              |                              |                              |
----------------------------------------------------------------------------------------------

Arithmetic Assignment Statement (Executable) 

Semantics 

If the type of the variable on the left of the equal sign differs from
that of the expression, type conversion takes place.  The expression is
evaluated and the result is converted to the type of the variable on the
left.  The converted result then replaces the current value of the
variable.  Conversion rules for the assignment statement are shown in
Table 3-3 , followed by examples in Table 3-4 .  See also Table 2-4
.

          Table 3-3.  Type Conversion for Arithmetic Assignment Statements 
                      of the Form:  Variable = Expression 
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
| Rule  |  Variable Type   |    Expression Type    |                    Rules                    |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  a.   | INTEGER*k        | INTEGER*n             | If k >= n, assign INTEGER*k.  If k < n,     |
|       | (or LOGICAL*k)   |                       | assign least significant byte or half word  |
|       | (see Note 3)     |                       | to variable.  See Note 1.                   |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  b.   | INTEGER*k        | REAL*n                | Truncation.                                 |
|       | (or LOGICAL*k)   |                       |                                             |
|       | (see Note 3)     |                       |                                             |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  c.   | INTEGER*k        | COMPLEX*8             | Real part is REAL*4.  Apply rule b to real  |
|       | (or LOGICAL*k)   |                       | part.  Imaginary part is not used.          |
|       | (see Note 3)     |                       |                                             |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  d.   | INTEGER*k        | COMPLEX*16            | Real part is REAL*8.  Apply rule b to real  |
|       | (or LOGICAL*k)   |                       | part.  Imaginary part is not used.          |
|       | (see Note 3)     |                       |                                             |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  e.   | REAL*k           | INTEGER*n             | Float and assign REAL*k.  See Note 2.       |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  f.   | REAL*k           | REAL*n                | Round and assign REAL*k.                    |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  g.   | REAL*k           | COMPLEX*8             | Real part is REAL*4.  Apply rule f to real  |
|       |                  |                       | part.  Imaginary part is not used.          |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  h.   | REAL*k           | COMPLEX*16            | Real part is REAL*8.  Apply rule f to real  |
|       |                  |                       | part.  Imaginary part is not used.          |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  i.   | COMPLEX*8        | INTEGER or REAL       | Convert to REAL*4 by rule e or f and assign |
|       |                  |                       | to real part.  Imaginary part = 0.          |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  j.   | COMPLEX*16       | INTEGER or REAL       | Convert to REAL*8 by rule e or f and assign |
|       |                  |                       | to real part.  Imaginary part = 0.          |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------
|       |                  |                       |                                             |
|  k.   | COMPLEX*k        | COMPLEX*n             | Apply rule f to real and imaginary parts.   |
|       |                  |                       |                                             |
--------------------------------------------------------------------------------------------------

Notes for Table 3-3  

   1.  If the value of the expression is between -32768 and +32767, the
       result of the conversion from INTEGER*4 to INTEGER*2 is correct;
       otherwise, the result is incorrect.

   2.  When converting from INTEGER*4 to REAL*4, the precision can be
       lost because INTEGER*4 holds 31 significant bits, while the number
       of significant bits for REAL*4 is system dependent.  Refer to
       Chapter 10  for more details on data representation.

   3.  As an extension to the ANSI 77 standard, logical variables
       appearing in an arithmetic context may be treated as integer
       variables, depending upon compiler directives.

In Table 3-4 , k and n represent examples of possible combinations of
byte sizes for the particular data type.  For example, if the variable is
INTEGER*k and the expression is REAL*n, k can be 2 or 4, while n can be
4, 8, or 16.  This represents six possible combinations of byte sizes.

          Table 3-4.  Examples of Type Conversions for Arithmetic Assignment Statements 
                      of the Form:  Variable = Expression 
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
| Rule  |  Variable Type   |    Variable Value     |      Expression Value      | Expression Type  |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  a.   | INTEGER*4        | 542                   | 542                        | INTEGER*2        |
|       | or LOGICAL*4     |                       |                            |                  |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  a.   | INTEGER*2        | Undefined (see Note   | 86420                      | INTEGER*4        |
|       | or LOGICAL*2     | 1).                   |                            |                  |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  b.   | INTEGER*2        | 3                     | 3.842                      | REAL*4           |
|       | or INTEGER*2     |                       |                            |                  |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  b.   | INTEGER*2        | 373                   | 373.7Q0                    | REAL*16          |
|       | or LOGICAL*2     |                       |                            |                  |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  c.   | INTEGER*2        | 502                   | (5.0297E2,1.27E-5)         | COMPLEX*8        |
|       | or LOGICAL*2     |                       |                            |                  |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  d.   | INTEGER*4        | -48170                | (-4.817D4,1.0096D7)        | COMPLEX*16       |
|       | or LOGICAL*4     |                       |                            |                  |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  e.   | REAL*4           | 59.                   | 59                         | INTEGER*2        |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  f.   | REAL*8           | 10.D+09               | 10.E+09                    | REAL*4           |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  f.   | REAL*4           | 1.7014E+38            | 1.7014118344D+38           | REAL*8           |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  h.   | REAL*4           | 8.425                 | (8.425,-6.02E-2)           | COMPLEX*8        |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  h.   | REAL*8           | 2.2964D-8             | (2.2964D-8,6.2881D-4)      | COMPLEX*16       |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  h.   | REAL*16          | 3.57Q297              | (3.57D297,-1.0D32)         | COMPLEX*16       |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  i.   | COMPLEX*8        | (50.0,0)              | 50                         | INTEGER*2        |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  i.   | COMPLEX*8        | (25.0,0)              | 25                         | REAL*4           |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  j.   | COMPLEX*16       | (14.23D-17,0.)        | 14.23E-17                  | REAL*4           |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  j.   | COMPLEX*16       | (1.0D28,0.0D0)        | 1.0000000000000000000635Q28| REAL*16          |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  k.   | COMPLEX*8        | (-4.817E4,1.0096E7    | (-4.817D4,1.0096D7)        | COMPLEX*16       |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------
|       |                  |                       |                            |                  |
|  k.   | COMPLEX*16       | (8.425D0,_6.02D-2)    | (8.425,-6.02E-2)           | COMPLEX*8        |
|       |                  |                       |                            |                  |
----------------------------------------------------------------------------------------------------

             Examples                                     Notes 

--------------------------------------------------------------------------------------

total = subtotal + tally           Defines the value of total as the value of subtotal
                                   + tally.

sum = sum + 1                      Replaces the value of sum with the value of sum +
                                   1.

rate(10) = new_rate * 5            Defines the 10th element of the array rate as the
                                   value of new_rate multiplied by 5.

Logical Assignment Statement (Executable) 

Semantics 

Both the variable and the expression must logical types in a logical
assignment statement.

                 Examples                                      Notes 

---------------------------------------------------------------------------------------

LOGICAL log1                                log1 is assigned the value .TRUE. because i
i = 10                                      equals 10.
log1 = i .EQ. 10

LOGICAL log_res, flag_set                   log_res is assigned the value .FALSE.
num = 100                                   because num is not greater than 200.
flag_set = .TRUE.
log_res = NUM .GT. 200 .AND. flag_set

Character Assignment Statement (Executable) 

Semantics 

If the length of the variable is greater than the length of the
expression, the value of the expression is left-justified in the
variable, and blanks are placed in the remaining positions.  If the
length of the variable is less than the length of the expression, the
value of the expression is truncated from the right until it is the same
length as the variable.

                 Examples                                      Notes 

---------------------------------------------------------------------------------------

CHARACTER*6 name                            The variable name is assigned the character
CHARACTER*4 instrument(6),k                 string "CYBELE".
name = 'CYBELE'
:

k = 'horn'                                  The fifth element of the array instrument
instrument(5) = k                           is assigned the character string "horn".
:

instrument(4) = name(3:5) // 'L'            The fourth element of the array instrument
                                            is assigned the character string "BELL".

CHARACTER*21 employee_name                  The variable employee_name is assigned the
employee_name = 'GEORGE WASHINGTON'         value "GEORGE Delta WASHINGTON Delta Delta
                                            Delta Delta ".

CHARACTER security_code*4                   The variable security_code is assigned the
security_code = 'ZXYwvu'                    value "ZXYw".

CHARACTER address*20                        The first through fourth characters of the
address (1:4) = '1645'                      variable address are assigned the value
address(6:14) = 'First St.'                 "1645" and the sixth through fourteenth the
                                            value "First St.".

Aggregate Assignment Statement (Executable) 

Semantics 

The field values of the aggregate on the left side of the equal sign are
assigned to the corresponding fields of the aggregate on the right side
of the equal sign.  The two aggregates must be declared with the same
structure name.

                 Examples                                      Notes 

---------------------------------------------------------------------------------------

STRUCTURE/student/                          A structure named student is declared with
CHARACTER*32 name                           two fields, name, a 32-byte character type,
INTEGER*2    age                            and age, a 2-byte integer type.
END STRUCTURE

STRUCTURE/teacher/                          A structure named teacher is declared with
CHARACTER*32 name                           two fields whose names and data types are
INTEGER*2    age                            identical to the structure student.
END STRUCTURE

RECORD/student/math_student                 Two records named math_student and
RECORD/student/english_student              english_student are declared using the
RECORD/teacher/math_teacher                 structure named student.  One record named
                                            math_teacher is declared using the
                                            structure named teacher.

math_student = english_student              This is a valid aggregate assignment
                                            statement, since both variables were
                                            declared with the same structure name.

math_student = math_teacher                 This is an illegal aggregate assignment
                                            statement, since the two variables were
                                            declared with different structure names,
                                            even though the two structures are
                                            identical in form.



MPE/iX 5.0 Documentation