Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
42 Views

Go To vs If Statement

I have two questions regarding the following legacy code segment:

      do i=1, n_total, increment  
         diff = array(i) - x1
         if (diff < 0.0_dp) then
         !   do something
            go to 10  
         else if (diff == 0.0_dp) then  
         !   do something
            go to 10
         else if (diff > 0.0_dp) then  
         !   do something
            go to 10
         end if  
      end do 

   ! We fell out of do loop, write error and stop

      write(unit=6,fmt='(''No Match'')')
      stop

   10 continue

1. Does Fortran 1990/95/03/08 provide a specific keyword or state variable (maybe something like END_STATUS) to check if a do loop completes without a branch or break or, in the case above, do you have to check the value of i against n_total?   The first approach would allow a standard set of code which would be independent of the index and terminal variable.

2. In the code above, would it be better to replace the go to statements with break and enclosed the error message in an if statement? Any performance difference between the two approaches?  What would be best practice?  Any other approach would be welcome.

Thanks....

0 Kudos
8 Replies
Highlighted
42 Views

loopblock: block
      do i=1, n_total, increment  
         diff = array(i) - x1
         if (diff < 0.0_dp) then
         !   do something
            exit loopblock
         else if (diff == 0.0_dp) then  
         !   do something
            exit loopblock
         else if (diff > 0.0_dp) then  
         !   do something
            exit loopblock
         end if  
      end do 

   ! We fell out of do loop, write error and stop

      write(unit=6,fmt='(''No Match'')')
      stop
   end block loopblock

! Execution continues here if something found

 

0 Kudos
Highlighted
Beginner
42 Views

Thanks Steve...the BLOCK construct looks great....

0 Kudos
Highlighted
Black Belt
42 Views

So far the "how" has been covered, but unless you have more things going on in the DO loop, the "why bother" question applies.

As it is, the DO loop will be executed only once, and the jump taken, because a real variable cannot be anything other than < 0, = 0 or > 0, unless you wish to cover the case where some of the variables involved are undefined.

0 Kudos
Highlighted
Beginner
42 Views

mecej4...the first conditional should be:

 

         if (diff < 0.0_dp) cycle

 

0 Kudos
Highlighted
Honored Contributor I
42 Views

Note under floating-point precision, the following statement is almost meaningless:

[fortran]

..

else if (diff == 0.0_dp) then

..

[/fortran]

0 Kudos
Highlighted
42 Views

mecej4,

The none of the above condition is: (isnan(diff))

FortranFan, (diff == 0.0_dp) almost meaningless

In the O.P's case, exact equality may be desired, chauvjo will have to determine this. In many applications it may be appropriate to use

real(dp), parameter :: AlmostZero = 1.0D-30 ! user specified
...
         if (diff < -AlmostZero) then
         !   do something
            exit loopblock
         else if (diff > AlmostZero) then  
         !   do something
            exit loopblock
         else then 
         ! -AlmostZero : AlmostZero 
         !   do something
            exit loopblock
         end if  

The above assumes NAN is never presented

Jim Demspey

0 Kudos
Highlighted
Black Belt
42 Views

The code presented has some of the flavor of translation of arithmetic IF.  Perhaps it assumes that abrupt underflow (/Qftz) is set so as to avoid accidental production of subnormals in place of zeroes.

0 Kudos
Highlighted
Beginner
42 Views

Tim P.: You are correct. The code segment I posed was a translation of an arithmetic IF. 

Jim: You have raised another issue on how best to handle an floating point equivalence check in an if statement.  Since this is off topic, I will start a new threat called "floating point equivalence check"

 

0 Kudos