Fortran DO loop sequence confusion

In summary, the conversation discusses converting a FORTRAN program into PERL and clarifies the process sequence in a specific section of the program. It also addresses possible typos in the code and explains the use of nested loops in FORTRAN.
  • #1
msmolen
6
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
  • #2
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
 
  • #3
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
 
  • #4
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
 
  • #5
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 )
 
  • #6
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.
 
  • #7
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
 
  • #8
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
  • #9
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

1. What is a Fortran DO loop sequence?

A Fortran DO loop sequence is a programming construct that allows for repeated execution of a set of statements until a certain condition is met. It is commonly used in Fortran programming to iterate through a series of data or perform a repetitive task.

2. How does a Fortran DO loop sequence work?

A Fortran DO loop sequence is initiated with the DO keyword, followed by a loop index variable, a starting value, an ending value, and an optional step value. The statements within the loop are then executed repeatedly, with the loop index variable being incremented or decremented with each iteration until the ending value is reached.

3. What is the difference between DO and DO WHILE in Fortran?

The main difference between DO and DO WHILE in Fortran is the condition for loop termination. A DO loop uses a fixed number of iterations, while a DO WHILE loop uses a logical expression to determine when to terminate the loop.

4. What are some common mistakes to avoid when using Fortran DO loop sequences?

Some common mistakes to avoid when using Fortran DO loop sequences include not properly initializing the loop index variable, using the wrong data type for the loop index variable, and not properly defining the condition for loop termination. It is also important to ensure that the loop index variable is updated correctly within the loop.

5. How can I improve the performance of my Fortran DO loop sequence?

To improve the performance of a Fortran DO loop sequence, it is important to minimize the number of statements within the loop and to avoid unnecessary calculations or function calls. It is also helpful to use the most efficient data types for the loop index variable and to properly set the loop termination condition to avoid unnecessary iterations.

Similar threads

  • Programming and Computer Science
Replies
12
Views
963
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
12
Views
3K
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top