How to remove goto statement (below code)

  • Thread starter petra
  • Start date
  • Tags
    Code
In summary, the code uses a for loop to calculate the values of VR, LR, and VS. The loop is incremented by 1 each time it is executed, and if the loop is greater than 50, the code will stop. The values of x(1) through x(nf+1) are the values of VR, LR, and VS after the for loop, respectively. The call statement calls the function x(nf+1). If x
  • #1
petra
8
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
  • #2
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
  • #3
  • #4
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
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
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.
 
  • #7
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:
  • #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:
    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
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 ?
 

1. How does the use of goto statements affect the readability of code?

The use of goto statements can make code more difficult to understand and follow, as it disrupts the natural flow of the program and can jump to different parts of the code without warning.

2. Are goto statements considered good practice in programming?

No, goto statements are generally considered a bad practice in programming as they can make code more complex and difficult to debug.

3. What are some alternatives to using goto statements?

Some alternatives to using goto statements include using loops, functions, and structured control flow statements such as if/else or switch statements.

4. How can goto statements be removed from existing code?

Goto statements can be removed by identifying the sections of code that use them and refactoring the code to use alternative methods such as loops or functions.

5. What are the potential risks of removing goto statements from code?

The main risk of removing goto statements from code is that it can introduce new bugs or errors if not done carefully. It is important to thoroughly test the code after removing goto statements to ensure it still functions as intended.

Similar threads

  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
Back
Top