Fortran DO loop sequence confusion

Click For Summary

Discussion Overview

The discussion revolves around understanding the sequence of operations in a FORTRAN program related to Canonical Correlates, specifically focusing on the structure and flow of DO loops. Participants are attempting to clarify the execution order of nested loops and the implications of specific lines of code, particularly in the context of converting the program to PERL.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks clarification on the sequence of operations in the FORTRAN code, particularly how the loops interact and the flow from one line to another.
  • Another participant points out potential typos in the original code, suggesting that line b) would not compile and that the logic of lines c) and d) appears incorrect.
  • A participant proposes a corrected version of the code to better reflect standard FORTRAN syntax, indicating that line b) should only have one '=' sign.
  • Some participants express confusion about the execution order, questioning whether the program processes all iterations of the outer loop before moving to the inner loop or if it alternates between them.
  • There are discussions about the semantics of the code, with some suggesting that the structure might be clearer if the inner loop iterated from I to M instead of 1 to M.
  • Concerns are raised about the initialization of variables and the overall logic of the program, with some participants questioning the cumulative nature of the assignments in lines b) and d).

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct interpretation of the code's execution sequence. There are multiple competing views regarding the structure and correctness of the FORTRAN code, as well as differing opinions on the implications of the syntax used.

Contextual Notes

Some participants note the potential for confusion due to the older style of FORTRAN syntax and the specific behavior of assignment statements, which may vary across programming languages.

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   Reactions: 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   Reactions: 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   Reactions: 1 person

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
8K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
6
Views
4K