DATA ICOUNT,PI/0,3.14159/A DATA statement consists of a list of variables, arrays and array elements terminated by a / followed by a list of values to be stored in them. In the above example ICOUNT would be initialized to 0 and PI to 3.14159. The above DATA statement is not the same as:-
ICOUNT=0
PI=3.14159for these are executable statements (see section 18) and reset ICOUNT and PI each time they are encountered where as the DATA statements have no effect while the program is running. For example, consider these two subroutines:-
SUBROUTINE A DATA ICALL/0/ ICALL=ICALL+1 PRINT *,ICALL RETURN ENDand
SUBROUTINE B ICALL=0 ICALL=ICALL+1 PRINT *,ICALL RETURN ENDEach time subroutine A is called it will type out an integer one higher than the previous time starting with the value 1. Subroutine B will always type out the value 1. Sometimes a routine needs to have code that is executed when the routine is called for the first time; DATA statements make this easy, for example:-
SUBROUTINE INPUT LOGICAL FIRST DATA FIRST/.TRUE./ IF (FIRST) THEN . . initialisation code . FIRST=.FALSE. ENDIF . . .
Array elements and complete arrays can also be initialised using DATA statements. The list of values in a DATA statement may also contain terms of the form :-
n*valuewhere n is positive integer and is equivalent to a list of n values. For example:-
DIMENSION A(7,5),B(4)
DATA A,B(3)/36*0./This initialises every element of A and the element 3 of B to zero. Part of an array can be initialised using the implied DO LOOP construction described in section 10.2.2. For example:-
DIMENSION C(2,3) DATA (C(1,I),I=1,3),(C(2,I),I=1,3,2)/3*1.0,2*2.0/