Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip iterations in a do-loop (fortran)

I want to loop over N iterations, but some of the iterations should be "skipped" under particular conditions. I know I can do it using the goto statement, such as :

       do i = 1, N
          if condition(i) goto 14
          ! Execute my iteration if condition(i) is false
    14    continue
       end do

But I am a little scared of these goto statements, and I would like to know if there is another solution (I am using fortran 90 but would be interested in any solution, even if it requires a newer version).

like image 342
Feffe Avatar asked May 11 '16 14:05

Feffe


2 Answers

Try this

do i = 1, N
          if (condition(i)) cycle
          ! Execute my iteration if condition(i) is false
end do

If you need explanation, comment on what you need clarification of. Note I've dropped the archaic continue and labelled statement.

like image 139
High Performance Mark Avatar answered Nov 11 '22 07:11

High Performance Mark


You can also to this:

   do i = 1, N
      if ( .not. condition(i) ) then
         ! Execute my iteration if condition(i) is false
      endif
   end do
like image 22
FredK Avatar answered Nov 11 '22 08:11

FredK