Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple infinite for loop 1

Status
Not open for further replies.

bobo12

Programmer
Dec 26, 2004
74
US
hi all,
i've been stuck on this for hours...
ok so consider the following code segment, there is code before and after it, but i DON'T understand the last line(20 NS = NS - 1) can ever execute. it seems to me that this code just is a while loop that keeps on repeating 8 NS = NS + 1 and never really goes to the code after(20 NS = NS - 1)
but i know it does b/c there is code after it, and every program should terminate, would some one plz help. please...


PROGRAM CODE BEFORE
...
8 NS = NS + 1
AA = 0.0
READ (11,FMAT3,END=20) (ITEM(J), J = 1,NI)
IF (ANS2 .EQ.'N' .OR. ANS2 .EQ. 'n') GO TO 17
DO 118 J = 1,NI
ITEM(J) = ITEM(J) / (10 ** IFMT(J))
IF (ITEM(J) .GT. 99) CALL PSTOP
IF (ITEM(J) .GT. 7) GO TO 118
IF (ITEM(J) .LE. 0) GO TO 118
IFRQ (J,ITEM(J)) = IFRQ(J,ITEM(J)) + 1
118 CONTINUE
17 IF(ANS .EQ. 'M' .OR. ANS .EQ. 'm')CALL SCOR(ITEM,NI,KEY)
C WRITE (7,888) (ITEM(JJ),JJ=1,NI)
C 888 FORMAT (2X, 40I1)
18 DO 10 J = 1,NI
A = ITEM(J)
SCOR1(J) = SCOR1(J) + A
SCOR2(J) = SCOR2(J) + A ** 2
AA = AA + A
10 CONTINUE
IF (ITMTOT .EQ. 0) GO TO 120
DO 110 J = 1,NI
AA1 = 0.0
DO 100 K = 1,NI
IF (K .EQ. J) GO TO 100
A1 = ITEM(K)
AA1 = AA1 + A1
100 CONTINUE
SUM2Q(J) = SUM2Q(J) + AA1 ** 2
110 CONTINUE

120 DO 15 J = 1,NI
A = ITEM(J)
A2 = AA - A
SUMXY(J) = SUMXY(J) + A2 * A
15 CONTINUE
SUM = SUM + AA
SUM2 = SUM2 + AA ** 2
IF (NSC .LE. 1) GO TO 8
DO 145 J = 1,NSC
NNN = NSCALE(J)
AA1 = 0.0
NN3 = 0
DO 140 K = 1,NI
DO 130 L = 1,NNN
IF (K .EQ. KK3(J,L)) GO TO 135
130 CONTINUE
GO TO 140
135 NN3 = NN3 + 1
A1 = ITEM(K)
SCOR1C(J,NN3) = SCOR1C(J,NN3) + A1
SCOR2C(J,NN3) = SCOR2C(J,NN3) + A1 ** 2
AA1 = AA1 + A1
140 CONTINUE
DO 125 K = 1,NNN
A1 = ITEM(KK3(J,K))
AA1 = AA1 - A1
SCAX1T(J,K) = SCAX1T(J,K) + AA1
SCAX2T(J,K) = SCAX2T(J,K) + AA1 ** 2
SUMXYC(J,K) = SUMXYC(J,K) + AA1 * A1
AA1 = AA1 + A1
125 CONTINUE
SCAX1(J) = SCAX1(J) + AA1
SCAX2(J) = SCAX2(J) + AA1 ** 2
145 CONTINUE
GO TO 8

PROGRAM CODE AFTER
20 NS = NS - 1

so under what condition is 20 NS = NS - 1 line executed?
 
See
Code:
READ (11,FMAT3,END=20) (ITEM(J), J = 1,NI)
The program goes to 20 on end of file condition.
So this loop will be terminated...
Well, it's a 'good' sample of old good Fortran style...
 
so ok, when does the line beginning with
IF (ANS2 .EQ.'N' .OR. ANS2 .EQ. 'n') GO TO 17
execute. i mean if

READ (11,FMAT3,END=20) (ITEM(J), J = 1,NI) is just a for loop that reads in values into item[j], then it will go to END of file(then line 20) and never reach IF (ANS2 .EQ.'N' .OR. ANS2 .EQ. 'n') GO TO 17

would u plz explain?
 
This READ() jumps to 20 ONLY if end of file condition raised (nothing read). If it read values (by its implicit loop) into ITEM, control passes to the next statement (IF(ANS... etc).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top