Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to remove goto statement (below code)

  1. Sep 16, 2015 #1
    Code (Fortran):
    100 VR=D+R
      LR=R
      VS = VR - (1-q)*F
      LS = LR +f*Q
      loop = loop +1
      if (loop.GT.50) stop
      x(1) = ..............
      CALL
      for i=2 to nf
      x(i) = ..............
      call
      x(nf+1) = (vr*y(nf)+b*xb....)
      if (x(nf+1).LT.x(nf)) go to 30
      call
      for i=nf+2 to nt
      x(i)=(vr*y(n-1)........)
      if  ( x(i).GT.Xd) goto 40
      call
      next i
      if(abs(y(nt)-xd).LT.0,0001) go to 90
      if (y(nt)-xd) 30,30,40
    30  if (flagp.lt.0.) dr=dr/2
      r=r+dr
      flagm = -1
      go to 100
    40  if (flagm.lt.0.) dr=dr/2
      r=r-dr
      flagp = -1
      goto 100
    90  write (6,91)
     
    Last edited by a moderator: Sep 16, 2015
  2. jcsd
  3. Sep 16, 2015 #2

    Svein

    User Avatar
    Science Advisor

    There is more problems with this code than "goto", but:

    Generally - when you write: "if (condition) goto nnn" you could instead use "if (not condition) then..". But since it seems that you are trying to program in an old version of FORTRAN, I have no idea of how to create a statement block in that language.
     
  4. Sep 16, 2015 #3

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

  5. Sep 16, 2015 #4

    jedishrfu

    Staff: Mentor

    Is this a homework assignment?

    What version of fortran is this? It looks like fortran-IV

    What compiler are you using? Or are you converting the code for a newer compiler?
     
  6. Sep 16, 2015 #5

    jtbell

    User Avatar

    Staff: Mentor

    Which version of Fortran has for-loops? All I've ever seen are do-loops.

    Also, a 'call' statement in Fortran always includes the name of a subroutine, as far as I know.
     
  7. Sep 16, 2015 #6

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Maybe the OP got confused as to which language this code is in. Fortran and Basic use similar commands.

    For loops and Next i suggest that this code might actually be written in some variant of BASIC, perhaps Visual Basic.

    In any event, the IF THEN statement constructions could also be used to replace the GO TO statements.
     
  8. Sep 16, 2015 #7

    wle

    User Avatar

    BASIC doesn't have an arithmetic IF statement.

    But even ignoring that, why are there two for i=... lines and only one next i?
     
    Last edited: Sep 16, 2015
  9. Sep 16, 2015 #8
    Well, I don't know Basic, but I guess it does not matter; I did pull up an index of Basic command in an attempt to get it right.

    I think it would look something like this:
    Code (Text):

        do
            VR=D+R
            LR=R
            VS = VR - (1-q)*F
            LS = LR +f*Q
            loop = loop +1
            if (loop.GT.50) stop
            x(1) = ..............
            CALL
            for i=2 to nf
            x(i) = ..............
            call
            x(nf+1) = (vr*y(nf)+b*xb....)
            if (x(nf+1).LT.x(nf))
                if (flagp.lt.0.) dr=dr/2
                r=r+dr
                flagm = -1
            else
                call
                for i=nf+2 to nt
                x(i)=(vr*y(n-1)........)
                if  ( x(i).GT.Xd)
                    if (flagm.lt.0.) dr=dr/2
                    r=r-dr
                    flagp = -1
                else
                    call
                    next i
                    if(abs(y(nt)-xd).LT.0,0001) exit
                    if(y(nt) <= xd
                        if (flagp.lt.0.) dr=dr/2
                        r=r+dr
                        flagm = -1
                    else
                        if (flagm.lt.0.) dr=dr/2
                        r=r-dr
                        flagp = -1
                    endif
                endif
            endif
        loop
        write (6,91)
     
     
  10. Sep 17, 2015 #9

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    In your IF THEN ELSE blocks, you omitted the keyword THEN after the condition, which should be like this:

    if (x(nf+1).LT.x(nf)) then
    .
    .
    .
    else
    .
    .
    .
    endif
     
  11. Sep 17, 2015 #10

    Mark44

    Staff: Mentor

    I'm hopeful that the OP will return to this thread. The OP has been a member here for a bit over 10 years. Except for the post in this thread, all other posts were made back in 2005. Let's hope he/she returns to this thread before 2025.
     
  12. Sep 18, 2015 #11

    jtbell

    User Avatar

    Staff: Mentor

    According to her(?) profile, she was "last seen" on Wednesday, at the time she posted. I think "last seen" includes visits (viewing pages while logged in). I agree the prudent course now is to wait for her return.
     
  13. Oct 9, 2015 #12

    Baluncore

    User Avatar
    Science Advisor

    At one time this code was Fortran IV. Then it got partly rewritten to Basic.

    Unfortunately there were two “Do line_number ... ” that have become “FOR i = ” and in the translation have lost their line numbers.
    There is now only one Next i.
    It appears to be a binary search.

    Petra; Do you have the original Fortran IV code ? Do you want structured Basic ?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: How to remove goto statement (below code)
  1. Goto statements (Replies: 25)

Loading...