Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop in Fortran from a list

I use Fortran and I was wondering if it's possible to make something like that

  do i = array
    write (*,*) i
  end do

where array is a list of integer numbers not necessarily ordered.

like image 306
Brian Avatar asked Mar 20 '12 16:03

Brian


People also ask

What is the difference between DO-loop and implied DO-loop in Fortran?

Implied DO loops are DO loops in the sense that they control the execution of some iterative procedure, but are different than DO loops because they do not use the do statement to control the execution.

How do you exit a loop in Fortran?

Exit statement terminates the loop or select case statement, and transfers execution to the statement immediately following the loop or select.

What is the general form of implied DO-loop?

It starts with a (, followed by a set of items, separated by commas, followed by a DO variable, an equal sign, an initial value, a final value, and a step-size, end ends with a ). Like a typical DO-loop, if the step size is 1, it can be eliminated.


1 Answers

I would introduce a second index to iterate over the elements of an array:

program test

  implicit none

  integer, dimension(6)  :: A
  integer, dimension(10) :: B
  integer                :: i, j

  A = (/ 1, 3, 4, 5, 8, 9 /)
  B = (/ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 /)

  do j = 1, size(A)
     i = A(j)
     write(*,*) i, B(i)
  end do

end program test
like image 165
alexurba Avatar answered Nov 04 '22 04:11

alexurba