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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

obsolete go to statements

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
0
0
US
Hi,

I am trying to modify code that is using go to statements everywhere. I want to use something more standard and also easier to read, but I am giving myself a headache trying to sort this out. Here is what I have:

Code:
      IMPLICIT NONE

      INTEGER i,igap,iex,ik,imax,ipl,j,k,l,n
      DOUBLE PRECISION sv,x(n),k(n)
*      dimension x(n),k(n)
      igap = n

      DO 1 i = 1,n
 1    k(i) = i
* 5    if(igap.le.1) go to 25
      IF(igap.le.1) THEN

*  now sort k's (for identical values of x, if any)
         j = 1 
      ELSE
         igap = igap/2
         imax = n - igap
      END IF
 10   iex = 0
      do 20 i = 1,imax
      ipl = i + igap
      IF(x(i).LE.x(ipl)) GO TO 20
      sv = x(i)
      ik = k(i)
      x(i) = x(ipl)
      k(i) = k(ipl)
      x(ipl) = sv
      k(ipl) = ik
      iex = iex + 1
 20   CONTINUE
      if(iex.gt.0) go to 10
      go to 5
c
c  now sort k's (for identical values of x, if any)
c
 25   j = 1
 30   if(j.ge.n) return
      if(x(j).eq.x(j+1)) go to 33
      j = j + 1
      go to 30
c  have at least two x's with the same value - see how long this is true
 33   l = j
 35   if(x(l).ne.x(l+1)) go to 38
      l = l + 1
      if(l.lt.n) go to 35
c  j and l are the indices within which x(i) does not change - sort k
 38   igap = l - j + 1
 40   if(igap.le.1) j = l + 1
      if(igap.le.1) go to 30
      igap = igap/2
      imax = l-j+1 - igap
 45   iex = 0
      do 50 i=1,imax
      ipl = i + igap + j - 1
      if(k(i+j-1).le.k(ipl)) go to 50
      ik = k(i+j-1)
      k(i+j-1) = k(ipl)
      k(ipl) = ik
      iex = iex + 1
 50   continue
      if(iex.gt.0) go to 45
      go to 40

I started to rewrite using IF ELSE statements, but I am not sure when one if-else-if should end and another begins. Code someone offer help to get me started?
 
It's nor easy to translate the old spaghetti code into structured code. You need to know what the program should do.

However, formally you can replace e.g
Code:
do <label_nr> <index=from, to>
<do_loop_body>
<label_nr> continue
with this
Code:
do <label_nr> <index=from, to>
  <do_loop_body>
end do

or this
Code:
if(<condition>) go to <label_nr>
<if_body>
<label_nr> <statement>
with this
Code:
if (.not.(<condition>)) 
  <if_body>
end if
<statement>
but without understanding exactly what the program should do, it's IMHO dangerous task.
 
Easiest way is to draw an old fashioned flowchart, pretend the start and end are two ends of a bit of string and pull it straight. Once you see the structure in a flowchart, it is quite easy to rewrite it.

Looks like a Shell sort (Shell-Metzner if you have older books). Have a look at The increment in the URL is the same as the gap variable in your cod.e
 
xwb,

It is probably not a coincidence that the subroutine I'm trying to modify is named shells! I will see what I can do with the link you provided.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top