How to remove goto statement (below code)

  • Thread starter Thread starter petra
  • Start date Start date
  • Tags Tags
    Code
Click For Summary

Discussion Overview

The discussion revolves around a piece of code that utilizes the "goto" statement, with participants exploring how to refactor it, particularly in the context of Fortran and possibly BASIC. The conversation touches on programming constructs, code structure, and the challenges of translating between programming languages.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • Some participants suggest replacing "goto" statements with structured programming constructs like "if ... then ... else" in Fortran.
  • There is uncertainty about the version of Fortran being used, with some suggesting it resembles Fortran IV.
  • Some participants question the use of "for" loops, noting that traditional Fortran typically employs "do" loops instead.
  • One participant proposes that the code may actually be written in a variant of BASIC, possibly Visual Basic, due to the syntax used.
  • There are discussions about the correct usage of "call" statements in Fortran, with some noting that they should include the name of a subroutine.
  • Participants express confusion over the structure of the code, particularly regarding the presence of multiple "for" statements without corresponding "next" statements.
  • One participant attempts to rewrite the code using a structured approach, but notes missing keywords and syntax issues in their proposed solution.
  • There are comments on the original context of the code, with a participant asking if the OP has the original Fortran IV code and if they want a structured BASIC version.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best way to refactor the code or the exact programming language it is intended for. Multiple competing views remain regarding the appropriate constructs to use and the interpretation of the code's syntax.

Contextual Notes

There are limitations in understanding the original code due to potential missing line numbers and the ambiguity of the programming language used. The discussion reflects a mix of Fortran and BASIC syntax, leading to confusion among participants.

Who May Find This Useful

This discussion may be useful for programmers interested in code refactoring, particularly those working with legacy Fortran or BASIC code, as well as those exploring structured programming techniques.

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   Reactions: 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   Reactions: 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 ?
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K