How to remove goto statement (below code)

  • Thread starter Thread starter petra
  • Start date Start date
  • Tags Tags
    Code
AI Thread Summary
The discussion revolves around a code snippet that appears to be a mix of FORTRAN and BASIC, with participants analyzing its structure and syntax. Key points include the use of "goto" statements, which are criticized in favor of structured programming practices like "if...then...else" constructs. Participants express confusion over the language used, questioning whether it is FORTRAN IV or a variant of BASIC, noting that FORTRAN typically utilizes "do" loops rather than "for" loops. There is also a focus on the proper use of "call" statements, which should include subroutine names. The conversation highlights the need for clarity in programming languages and the importance of maintaining structured code to avoid errors. The original poster's coding issues are acknowledged, with suggestions for improvements and a hope for their return to the thread for further discussion.
petra
Messages
8
Reaction score
0
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:
Technology news on Phys.org
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.
 
  • Like
Likes aikismos
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?
 
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.
 
jtbell said:
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.
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.
 
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:
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:
    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)
 
gsal said:
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:
    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)

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
 
  • #10
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.
 
  • Like
Likes ChrisVer and aikismos
  • #11
Mark44 said:
I'm hopeful that the OP will return to this thread.

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.
 
  • #12
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 ?
 
Back
Top