# How to remove goto statement (below code)

1. Sep 16, 2015

### petra

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. Sep 16, 2015

### Svein

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.

3. Sep 16, 2015

### SteamKing

Staff Emeritus
4. Sep 16, 2015

### 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?

5. Sep 16, 2015

### 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.

6. Sep 16, 2015

### SteamKing

Staff Emeritus
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.

7. Sep 16, 2015

### wle

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
8. Sep 16, 2015

### gsal

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)

9. Sep 17, 2015

### SteamKing

Staff Emeritus
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. Sep 17, 2015

### 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.

11. Sep 18, 2015

### 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.

12. Oct 9, 2015

### Baluncore

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 ?