Modifying iteration of DO-loop dynamically

  • Thread starter chaim
  • Start date
In summary, the speaker is trying to change the start integer for a DO-loop each time it runs based on the calculations of the previous iteration. They have tried to do this dynamically but it results in a compiler error. They ask for alternative logic or methods to modify the loop index variable. The expert suggests using a DO WHILE loop instead of a regular DO loop as it allows for modification of the loop index variable within the loop.
  • #1
chaim
5
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
  • #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.
 
  • #3
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.
 

1. What is a DO-loop and why is it used?

A DO-loop is a control structure used in programming to repeat a set of instructions for a specific number of times or until a certain condition is met. It is commonly used to iterate through a block of code and perform the same operations on different sets of data.

2. How can the iteration of a DO-loop be modified dynamically?

The iteration of a DO-loop can be modified dynamically by using variables or expressions in the loop control statement. This allows the number of iterations to be determined during the execution of the program, based on the current state of the program or user input.

3. Can the iteration of a DO-loop be changed within the loop itself?

Yes, the iteration of a DO-loop can be changed within the loop itself by using control statements such as EXIT or CONTINUE. These statements allow the program to skip iterations or terminate the loop prematurely, based on certain conditions.

4. Are there any limitations to dynamically modifying the iteration of a DO-loop?

Yes, there are some limitations to dynamically modifying the iteration of a DO-loop. For example, if the loop control statement is dependent on a variable that is also used within the loop, it can lead to unexpected results. Careful consideration must be given to the logic of the program to avoid such issues.

5. What are the benefits of using dynamically modified iteration in a DO-loop?

Using dynamically modified iteration in a DO-loop can make the program more flexible and adaptable. It allows for more efficient use of system resources by only performing the necessary number of iterations. It also allows for more complex and dynamic programming, as the loop can be controlled by user input or other variables.

Similar threads

  • Programming and Computer Science
Replies
7
Views
7K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
27
Views
6K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
Back
Top