Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Modifying iteration of DO-loop dynamically

  1. Jan 23, 2011 #1
    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

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


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

    How can I modify AC without getting a compiler error or is an alernative logic necessary??
  2. jcsd
  3. Jan 23, 2011 #2
    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.
  4. Jan 23, 2011 #3


    User Avatar

    Staff: Mentor

    Or use a DO WHILE loop. The following two loops are equivalent:

    Code (Text):

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

          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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Modifying iteration of DO-loop dynamically
  1. Do while loop (Replies: 6)

  2. Fortran Help (Do Loop) (Replies: 3)