FORTRAN: 2 conditions in 1 DO statement

In summary: This saves the trouble of having to write out 10 different write statements.In summary, the conversation discussed the possibility of combining two DO loops into one to increment simultaneously, and the potential use of functions to simplify the code.
  • #1
RJLiberator
Gold Member
1,095
63

Homework Statement



I am working on a code that sends a 6480 x 10 data set into a 64800 line LAT / LON / DATA gridded set.

In part of my code, I'd like to have 2 conditions in 1 DO statement and I am not sure if it is possible.

Homework Equations

The Attempt at a Solution


[/B]
DO x = 1, 180
vmin=vmin+1
DO y = 1, 360, 10
DO v = vmin, vmin+35, 1

I'd like to combine DO y=1, 360, 10 and DO v=vmin, vmin+35, 1 so they increment simultaneously. Is this possible?
 
Physics news on Phys.org
  • #2
RJLiberator said:

Homework Statement



I am working on a code that sends a 6480 x 10 data set into a 64800 line LAT / LON / DATA gridded set.

In part of my code, I'd like to have 2 conditions in 1 DO statement and I am not sure if it is possible.

Homework Equations

The Attempt at a Solution


[/B]
DO x = 1, 180
vmin=vmin+1
DO y = 1, 360, 10
DO v = vmin, vmin+35, 1

I'd like to combine DO y=1, 360, 10 and DO v=vmin, vmin+35, 1 so they increment simultaneously. Is this possible?
You might be able to combine both DO loops into one, as the y and v loops execute 36 times. However, I'm puzzled by the y loop, as it starts off at 1, and is incremented by 10 each iteration. That is, it will take on the values 1, 11, 21, ..., 351.

I think the 2nd and 3rd DO loops in your code could be rewritten more simply like this:
Fortran:
y = 1
v = vmin
DO i = 1, 36
   ! stuff you want to have happen
   y = y + 10
   v = v + 1
END DO

Since you asked only about the y and v loops, I have addressed only those two. From what you wrote, I can't tell if the 2nd and 3rd loops are supposed to be inside the loop on x or not.

Also, when you post code, please use code tags, similar to what I did. What you type will look like this, but will render in a browser like my example above.
[code=fortran]
your code
[/code]
 
  • #3
Thanks for your reply.

The reason I had an increment of 10 was that I need to write out my data like so:

Fortran:
         write(11,666)x,y,j1(v)  
           write(11,666)x,y+1,j2(v)  
           write(11,666)x,y+2,j3(v)  
           write(11,666)x,y+3,j4(v)  
           write(11,666)x,y+4,j5(v)  
           write(11,666)x,y+5,j6(v)
           write(11,666)x,y+6,j7(v)  
           write(11,666)x,y+7,j8(v)  
           write(11,666)x,y+8,j9(v)  
           write(11,666)x,y+9,j10(v)

That way, y=1 will then take on values 1, 2, 3, 4,...10, then when y increments by 10, it will take on values 11, 12, 13... 20.

Your suggestion nailed it.

Now I've only have to do with formatting issues, otherwise everything seems to be working properly.
 
  • #4
RJLiberator said:
Thanks for your reply.

The reason I had an increment of 10 was that I need to write out my data like so:

Fortran:
         write(11,666)x,y,j1(v)
           write(11,666)x,y+1,j2(v)
           write(11,666)x,y+2,j3(v)
           write(11,666)x,y+3,j4(v)
           write(11,666)x,y+4,j5(v)
           write(11,666)x,y+5,j6(v)
           write(11,666)x,y+6,j7(v)
           write(11,666)x,y+7,j8(v)
           write(11,666)x,y+8,j9(v)
           write(11,666)x,y+9,j10(v)

That way, y=1 will then take on values 1, 2, 3, 4,...10, then when y increments by 10, it will take on values 11, 12, 13... 20.

Your suggestion nailed it.

Now I've only have to do with formatting issues, otherwise everything seems to be working properly.
There must be a cleaner way to do this. Are the j1( ), j2(), etc. calls to functions? At first I thought they might be arrays, but for an array the index v has to be an integer. The code below assumes that j1, j2, etc. are the names of functions.

Fortran:
real function jay(i, vel)
integer i
real vel
if (i .eq. 1)    ! calculate the value that j1(v) would have returned
   jay = ...
else if (i .eq. 2)   ! calculate the value that j2(v) would have returned
   jay = ...
...     ! and so on
end if
end function

BTW, j is a terrible name for a function in that i and j are used almost exclusively as loop control variables in DO loops.

Now the output:
Fortran:
do i = 1, 10
   write(11, 666) x, y + i, jay(i, v)
end do
 

Related to FORTRAN: 2 conditions in 1 DO statement

What is FORTRAN?

FORTRAN (short for Formula Translation) is a high-level programming language used for scientific and engineering applications. It was one of the first programming languages created and is still widely used today.

What is a DO statement in FORTRAN?

A DO statement is a loop control statement in FORTRAN that executes a set of instructions repeatedly until a specified condition is met. It is used to simplify repetitive tasks in a program.

Can multiple conditions be used in a single DO statement in FORTRAN?

Yes, it is possible to use multiple conditions in a single DO statement in FORTRAN. This can be achieved by combining the conditions using logical operators such as .AND. and .OR.

What is the syntax for using 2 conditions in a DO statement in FORTRAN?

The syntax for using 2 conditions in a DO statement in FORTRAN is as follows: DO i = start, stop, [step] [EXIT [condition1] [, condition2]] where condition1 and condition2 are the two conditions to be evaluated.

How are the conditions evaluated in a DO statement in FORTRAN?

The conditions in a DO statement in FORTRAN are evaluated sequentially from left to right. If the first condition is true, the loop will be executed. If the first condition is false, the second condition will be evaluated. If both conditions are false, the loop will terminate.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
753
  • Engineering and Comp Sci Homework Help
Replies
7
Views
944
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
Back
Top