Modifying iteration of DO-loop dynamically

  • Thread starter Thread starter chaim
  • Start date Start date
AI Thread Summary
The discussion centers on dynamically changing the start integer for a DO-loop in Fortran, specifically using the g77 compiler. The user aims to adjust the starting point of the loop based on calculations from previous iterations, with the first iteration starting at 1, the second at 33, and the third at 60, while keeping the end value constant. Attempts to modify the iterator directly within the DO-loop resulted in compiler errors. The solution proposed suggests that instead of using a standard DO-loop, one can utilize GOTO and IF statements, allowing for the modification of the loop index variable freely. Alternatively, a DO WHILE loop is recommended, as it permits changes to the loop index within the loop body. This approach provides the flexibility needed to achieve the desired dynamic behavior without encountering compiler issues.
chaim
Messages
5
Reaction score
0
I would like to change the start integer for a DO-loop each time the DO runs as a result of the calculations of the previous iteration. For example the first time the DO runs it should start at 1, the second time at 33, the third time at 60. The numerical value for the end of the loop stays the same. I have tried to change the iterator dynamically as can be seen in the code below, but the g77 gives an error.

ACS = 1
NUME = 0
DO 12, AC = ACS,NH

IF (VI(AC) .NE. 100.0) THEN
VF(AC) = VI(AC)
GOTO 12

ELSEIF (VI(AC) .EQ. 100.0) THEN
DO 80,AB = AC,AC+24
IF (VI(AB) .EQ. 100) NUME = NUME + 1
IF (VI(AB) .NE. 100) GOTO 81

80 CONTINUE
81 IF ((VI(AC-1)- VI(AC+NUME)) .GT. 0.0) THEN
DO 77, VAL = AC,AC+NUME+1
VF(VAL)=VI(AC)-((VI(AC-1)-VI(AC+NUME))/(NUME+1))

77 CONTINUE

ELSEIF ((VI(AC-1)- VI(AC+NUME)) .LT. 0.0) THEN
NUM = 0
DO 99, VAL = AC,AC+NUME-1
NUM = NUM + 1
VF(VAL)=VF(AC+NUM-2)+((VI(AC+NUME)-VI(AC-1))/(NUME+1))
AC = VAL -1 <=== compiler error
99 CONTINUE

How can I modify AC without getting a compiler error or is an alernative logic necessary??
 
Technology news on Phys.org
You cannot do it with a do loop. You must construct one out of goto and if statements. You are then free to modify the dummy variable any way you like.
 
Or use a DO WHILE loop. The following two loops are equivalent:

Code:
      do 99 k = start, stop
          print *, k
99    continue

Code:
      k = start
      do 99 while (k .le. stop)
          print *, k
          k = k + 1
99    continue

except that with the DO WHILE loop you can modify the loop index variable inside the loop (in fact, that's what this example does!) whereas with the regular DO loop you can't.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top