Fortran Fortran DO loop sequence confusion

AI Thread Summary
The discussion centers on understanding the sequence of operations in a FORTRAN DO loop while converting a program into PERL. The original code has syntax issues, particularly in line b, which should be corrected to avoid compilation errors. The correct flow involves executing line b for each I value, followed by the nested loop from lines c to e for each J value. The participants clarify that the outer loop processes all M values of I before moving to the inner loop for J, effectively creating a nested loop structure. Overall, the confusion primarily lies in the interpretation of the loop sequence and the correct syntax for FORTRAN.
msmolen
Messages
6
Reaction score
0
I am trying to convert a FORTRAN version of a Canonical Correlates program listed in Multivariate Morphology - Blackith and Reyment. I've programed in FORTRAN decades ago and now I have to understand the language to rewrite the program into PERL.

a) DO 260 I = 1 , M
b) SX( I ) = SX( I ) = SX( I ) + XX( I )
c) DO 260 J = 1 , M
d) SS( I , J ) = SS( I , J ) + XX( I ) * XX( J )
e) 260 SS( J , I ) = SS( I , J )

What is the process sequence here? When the program moves into a) the first time line a) moves to line b) is this done with I = 1 (first to M) and then move directly to line c) (J = 1) for processing to d), and then move to line e) and then jump back to a) for a repeat of this process ( I = 2 , M and J = 2 , M)?

I don't have a FORTRAN compiler to figure this out.
I've never seen multiple processing events (I = 1 to M) in a loop.
 
Technology news on Phys.org
I think you made some typos in your post. Line b) won't even compile, and likes c) and d) both look wrong (guessing what the code it supposed to be calculating)

But ignoring those issues, It might be easier to understand how the loops work in more Fortran style:
Code:
DO I = 1 , M
   SX( I ) = SX( I ) = SX( I ) + XX( I )
   DO J = 1 , M
      SS( I , J ) = SS( I , J ) + XX( I ) * XX( J )
      SS( J , I ) = SS( I , J )
   END DO
END DO
 
AlephZero - thank you for your quick reply. Sorry for the redundancy in line line b).

So, is this the correct sequence of events for the code in my original post:

first cycle for I = 1 , 10
line a) with I = 1
line b) with I = 1
line c) with J = 1
line d) with I = 1 , J = 1
line e) with J = 1 , I = 1

second cycle for I = 2 , 10

line a) with I = 2
line b) with I = 2
line c) with J = 2
line d) with I = 2 , J = 2
line e) with J = 2 , I = 1

next cycle for I = 3 , 10

Am I still confused?
mike
 
msmolen said:
Am I still confused?
This is like nested loops in any other proramming language.

first cycle for I = 1
line a) with I = 1
line b) with I = 1
line c) with J = 1
line d) with I = 1 , J = 1
line e) with J = 1 , I = 1
line c) with J = 2
line d) with I = 1 , J = 2
line e) with J = 2 , I = 1
...
line c) with J = 10
line d) with I = 1 , J = 10
line e) with J = 10 , I = 1

second cycle for I = 2

line a) with I = 2
line b) with I = 2
line c) with J = 1
line d) with I = 2 , J = 1
line e) with J = 1 , I = 2
...
line c) with J = 10
line d) with I = 2 , J = 10
line e) with J = 10 , I = 2

...

last cycle for I = 10

line a) with I = 10
line b) with I = 10
line c) with J = 1
line d) with I = 10 , J = 1
line e) with J = 1 , I = 10
...
line c) with J = 10
line d) with I = 10 , J = 10
line e) with J = 10 , I = 10
 
msmolen said:
I am trying to convert a FORTRAN version of a Canonical Correlates program listed in Multivariate Morphology - Blackith and Reyment. I've programed in FORTRAN decades ago and now I have to understand the language to rewrite the program into PERL.

a) DO 260 I = 1 , M
b) SX( I ) = SX( I ) = SX( I ) + XX( I )
c) DO 260 J = 1 , M
d) SS( I , J ) = SS( I , J ) + XX( I ) * XX( J )
e) 260 SS( J , I ) = SS( I , J )

What is the process sequence here? When the program moves into a) the first time line a) moves to line b) is this done with I = 1 (first to M) and then move directly to line c) (J = 1) for processing to d), and then move to line e) and then jump back to a) for a repeat of this process ( I = 2 , M and J = 2 , M)?

I don't have a FORTRAN compiler to figure this out.
I've never seen multiple processing events (I = 1 to M) in a loop.

Also, in general, you are allowed one '=' in an assignment statement, so for b) it appears the correct code would be:

SX( I ) = SX( I ) + XX( I )
 
msmolen said:
a) DO 260 I = 1 , M
b) SX( I ) = SX( I ) = SX( I ) + XX( I )
c) DO 260 J = 1 , M
d) SS( I , J ) = SS( I , J ) + XX( I ) * XX( J )
e) 260 SS( J , I ) = SS( I , J )

AlephZero said:
I think you made some typos in your post. Line b) won't even compile, and likes c) and d) both look wrong (guessing what the code it supposed to be calculating)
Line c looks OK to me - it's just the older style (Fortran 77?) where DO includes a statement label, in this case line e, where 260 is the label.

SteamKing said:
Also, in general, you are allowed one '=' in an assignment statement, so for b) it appears the correct code would be:
SX( I ) = SX( I ) + XX( I )
I believe this was a typo on the OP's part.

I don't remember whether Fortran allows only one '=' in an assignment statement. Other languages based on C (C, C++, C#, Java, JavaScript, and others) can have assignment statements with more than one variable being assigned a value.
 
Thanks for the feedback! I didn't create an unanticipated error when I transcript the code (line b). When I put fake data into this code it appears that the correct matrix formed indicates that the first DO loop is done from a) to b) - all M of them. and then moves to the next DO loop to process c) to e). But it still doesn't make sense. There is something to be said to have FORTRAN.
mike
 
Mark44 said:
Line c looks OK to me - it's just the older style (Fortran 77?) where DO includes a statement label, in this case line e, where 260 is the label.

It will compile and run, but if you look at the semantics of lines d) and e), it would make more sense if the loop in line c) ran from I to M, not 1 to M.

Even without a spec to read, if I was reviewing those lines of code I would put a big question mark in the margin.
 
Last edited:
  • Like
Likes 1 person
Mark44 said:
I don't remember whether Fortran allows only one '=' in an assignment statement. Other languages based on C (C, C++, C#, Java, JavaScript, and others) can have assignment statements with more than one variable being assigned a value.

There may be non-standard versions of Fortran which allow a series of assignments on one line with multiple '=' signs, but the standard allows only 'A = B' assignment statements.
 
  • #10
msmolen said:
I am trying to convert a FORTRAN version of a Canonical Correlates program listed in Multivariate Morphology - Blackith and Reyment. I've programed in FORTRAN decades ago and now I have to understand the language to rewrite the program into PERL.

a) DO 260 I = 1 , M
b) SX( I ) = SX( I ) = SX( I ) + XX( I )
c) DO 260 J = 1 , M
d) SS( I , J ) = SS( I , J ) + XX( I ) * XX( J )
e) 260 SS( J , I ) = SS( I , J )

What is the process sequence here? When the program moves into a) the first time line a) moves to line b) is this done with I = 1 (first to M) and then move directly to line c) (J = 1) for processing to d), and then move to line e) and then jump back to a) for a repeat of this process ( I = 2 , M and J = 2 , M)?

I don't have a FORTRAN compiler to figure this out.
I've never seen multiple processing events (I = 1 to M) in a loop.

General comment: Presumably SX(I), SS(I,J) have been initialized, XX(I) set previously.
Line e) is confusing- what exactly do you mean?

In fact the entire program looks flakey.
You seem to be setting SX(I) = XX(I) and SS(I,J) = XX(I)*XX(J). There is no cumulative sum for either.
 
  • #11
The confusion I think that I have with this code is the stepwise process. Does the program begin at the a) statement - a DO loop, moving to b) were the work gets done (originally I added an unintentional extra SX( I )) I should have just described b) SX( I ) = SX( I ) + XX( I ). Does the program than wallow in a) - b) doing it M times, then moving to c) to e)?

or

Does the program begin at a), move to b) for one processing step, then on to c) beginning another DO loop, went it then moves to d) and e), then jumping back to the start a) for another round with I now equal to 2?

mike
 
  • #12
msmolen said:
The confusion I think that I have with this code is the stepwise process. Does the program begin at the a) statement - a DO loop, moving to b) were the work gets done (originally I added an unintentional extra SX( I )) I should have just described b) SX( I ) = SX( I ) + XX( I ). Does the program than wallow in a) - b) doing it M times, then moving to c) to e)?

or

Does the program begin at a), move to b) for one processing step, then on to c) beginning another DO loop, went it then moves to d) and e), then jumping back to the start a) for another round with I now equal to 2?

mike
A better example might be helpful.
Code:
    DO 20 M = 1, 4    ; 1
      DO 10 N = 1, 3  ; 2
         PRINT*, M, N  ; 3
10  CONTINUE         ; 4
20 CONTINUE          ; 5

This is an example of a nested DO loop, similar to the one you posted, but easier to understand. The outer DO loop (lines 1 through 5) executes four times, for values of M of 1, 2, 3, and 4.
Each iteration of the outer DO causes the inner DO loop (lines 2 through 4) to execute three times

So, when M = 1, the inner loop cycles through values of N of 1, 2, and 3.
Then, when M = 2, the inner loop again cycles through the three N values.
When M = 3, the inner loop cycles through the three N values.
When M = 4, the inner loop cycles through the three N values one more time.

Each time one of the labeled CONTINUE statements is executed, the associated loop control variable is incremented by 1. When the loop control variable exceeds the limiting number (4 for the outer loop and 3 for the inner loop), the loop is finished.

All in all, the print statement (line 3) executes 4 * 3 = 12 times. The output looks like this:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
That's 12 pairs of numbers.
 
  • Like
Likes 1 person
  • #13
msmolen said:
The confusion I think that I have with this code is the stepwise process. Does the program begin at the a) statement - a DO loop, moving to b) were the work gets done (originally I added an unintentional extra SX( I )) I should have just described b) SX( I ) = SX( I ) + XX( I ). Does the program than wallow in a) - b) doing it M times, then moving to c) to e)?

or

Does the program begin at a), move to b) for one processing step, then on to c) beginning another DO loop, went it then moves to d) and e), then jumping back to the start a) for another round with I now equal to 2?

mike
DO loops work as follows:
For each I, do step b, then do steps c,d,e M times (once for each J), before going to the next I.
 
Last edited:
  • Like
Likes 1 person

Similar threads

Replies
12
Views
1K
Replies
6
Views
2K
Replies
8
Views
4K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
6
Views
2K
Replies
9
Views
2K
Back
Top