The DATA statement assigns initial values to variables before program execution. It takes the following form:
Subscript expressions and expressions in substring references must be initialization expressions.
An implied-do list in a DATA statement takes the following form:
A constant can be specified in the form r*constant, where r is a repeat specification. It is a nonnegative scalar integer constant (with no kind parameter). If it is a named constant, it must have been declared previously in the scoping unit or made accessible through use or host association. If r is omitted, it is assumed to be 1.
Rules and Behavior
A variable can be initialized only once in an executable program. A variable that appears in a DATA statement and is typed implicitly can appear in a subsequent type declaration only if that declaration confirms the implicit typing.
The number of constants in c-list must equal the number of variables in var-list. The constants are assigned to the variables in the order in which they appear (from left to right).
The following objects cannot be initialized in a DATA statement:
Except for variables in named COMMON blocks, a named variable has the SAVE attribute if any part of it is initialized in a DATA statement. You can confirm this property by specifying the variable in a SAVE statement or a type declaration statement containing the SAVE attribute.
When an unsubscripted array name appears in a DATA statement, values are assigned to every element of that array in the order of subscript progression. The associated constant list must contain enough values to fill the array.
Array element values can be initialized in three ways: by name, by element, or by an implied-do list (interpreted in the same way as a DO construct).
The following conversion rules and restrictions apply to variable and constant list items:
When the constant and variable conform to these restrictions, the variable is initialized with the character that has the ASCII code specified by the constant. (This lets you initialize a character object to any 8-bit ASCII code.)
If the Hollerith or character constant contains fewer characters than the capacity of the variable or array element, the constant is extended on the right with blank characters. If the constant contains more characters than can be stored, the constant is truncated on the right.
The following example shows the three ways that DATA statements can initialize array element values:
DIMENSION A(10,10) DATA A/100*1.0/ ! initialization by name DATA A(1,1), A(10,1), A(3,3) /2*2.5, 2.0/ ! initialization by element DATA ((A(I,J), I=1,5,2), J=1,5) /15*1.0/ ! initialization by implied-do list
The following example shows DATA statements containing structure components:
TYPE EMPLOYEE INTEGER ID CHARACTER(LEN=40) NAME END TYPE EMPLOYEE TYPE(EMPLOYEE) MAN_NAME, CON_NAME DATA MAN_NAME / EMPLOYEE(417, 'Henry Adams') / DATA CON_NAME%ID, CON_NAME%NAME /891, "David James"/
In the following example, the first DATA statement assigns zero to all 10 elements of array A, and four asterisks followed by two blanks to the character variable STARS:
INTEGER A(10), B(10) CHARACTER BELL, TAB, LF, FF, STARS*6 DATA A,STARS /10*0,'****'/ DATA BELL,TAB,LF,FF /7,9,10,12/ DATA (B(I), I=1,10,2) /5*1/
In this case, the second DATA statement assigns ASCII control character codes to the character variables BELL, TAB, LF, and FF. The last DATA statement uses an implied-do list to assign the value 1 to the odd-numbered elements in the array B.
As a Fortran 95 feature, a pointer can be initialized as disassociated by using a DATA statement. For example:
INTEGER, POINTER :: P DATA P/NULL( )/ END
For More Information: