Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What does this construction mean? 1

Status
Not open for further replies.

NickFort

Technical User
Jun 10, 2010
113
I've been looking at the code in the DVODE_F90 module I mentioned in another thread.

It was translated from F77 to F90, I believe, so some older constructions exist. What does the following do with the "60"? "DO 60", "GOTO 60" pointing to "END DO".

Code:
        DO 60 I = 1, NGC
          IF (ABS(G0(I))>ZERO) GOTO 60
          JROOT(I) = 1
          ZROOT = .TRUE.
60      END DO

Is it equivalent to?:

Code:
        DO I = 1, NGC
          IF (ABS(G0(I))>ZERO) EXIT
          JROOT(I) = 1
          ZROOT = .TRUE.
        END DO

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
No, it is the same as
Code:
      DO 60 I = 1, NGC
          IF (ABS(G0(I)) .le. ZERO) THEN
          JROOT(I) = 1
          ZROOT = .TRUE.
          ENDIF
      END DO
or
Code:
      DO 60 I = 1, NGC
          IF (ABS(G0(I)) > ZERO) CYCLE
          JROOT(I) = 1
          ZROOT = .TRUE.
      END DO
It is negative logic - quite common on programs which don't use the if-then construct. This one just marks the ones that are zero. Your second piece of code exits as soon as the first one is seen.
 
Sorry, no idea where "construction" term came from. :p Don't I feel like an idiot...

Anyway, regarding the two snippets of code in your post -- do they even need the "60" in the "DO" line, seeing as now "60" isn't used anywhere?

Also, is there any good reason not to use the if-then construct?

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
The 60 is not required if you are not using a goto.

What you use is entirely up to you. Some people prefer using cycle, others prefer the if-then. It also depends on how many conditions you are checking and the endloop conditions. You could have
Code:
do ...                        do ...
   if (AAA) then                 if (.not. AAA) cycle
      ...                        ...
      if (BBB) then              if (.not. BBB) cycle
         ...                     ...
         if (CCC) then           if (.not. CCC) cycle
            ...                  ...
         end if
      end if
   end if
   ! Use this if there is        ! Cannot handle endloop
   ! common end-loop code        ! code
end do                        end do
As you can see, using cycle is a lot shorter but some people prefer the nesting because they say it is clearer. Just a matter of personal taste.

If you use cycle, the end loop stuff will have to be at the beginning of the loop.
 
Hmmm... Interesting! I've actually never heard of that way of doing things before. Thanks for enlightening me!

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
It gets worse - there are people who do not like else if (why?). They're very proud of this but I really hate it. You may see similar things (do while (false) loops) in C/C++.
Code:
do
   if (AAA) then                   if (AAA) then
      ...                             ...
      exit
   end if

   if (BBB) then                  else if (BBB) then
      ...                             ...
      exit
   end if

   if (CCC) then                  else if (CCC) then
      ...                             ...
      exit
   end if                         else
   ...                                ...
   exit                           end if
enddo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top