GO TO statements- Fortran to Matlab

前端 未结 1 1253
走了就别回头了
走了就别回头了 2021-01-29 09:31

I have been trying really hard to convert this grid search code from Fortran to Matlab, however I am not able to properly incorporate the GO TO statements (I am trying to use wh

相关标签:
1条回答
  • 2021-01-29 10:14

    You have two types of control-flow implemented with your GOTO statement:

    Type 1: breaking out of a loop

    The GOTO statement is used to break out of the DO-loop:

        DO acc0 = 1,intA+1  
            ! some code
            IF (cons<0.0) GOTO 102
            ! some more code
        END DO
    102 continue
    

    As you notice, if cons < 0.0 the GOTO statement states to move to label 102 which is located just outside of the DO-loop. In matlab is nothing more than a simple break in a for-loop:

    for acc0=1:intA+1
      % some matlab code
      if (cons < 0.0)
        break
      end
      % some more matlab code
    end
    

    Type 2: creating a loop

    While the loop is not explicitly written, the following code creates something that can be translated as a while loop:

        ! This is the start of the loop
    102 continue
        ! This is the condition to exit the loop <<==
        IF (askip<2) GO TO 109
        ! stuff
        ! This is a condition to restart the loop    <<==
        IF (vtemp>vmax) THEN
             vmax   = vtemp
             amax_G = acc
             GOTO 102
        ENDIF
        ! stuff
        ! This is another condition to restart the loop   <<==
        IF (cons<0.0) GO TO 102
        ! stuff
        ! This is the end of the loop, asked to restart it  <<==
        GOTO 102
        ! This is outside of the loop
    109 CONTINUE
    

    In the end, you translate this as:

    while (askip >=2)
       % stuff
       if (vtemp > vmax)
            vmax = vtemp
            amax_G = acc
            continue
       end
       % stuff
       if (cons < 0.0)
          continue
       end
       % stuff
    end
    
    0 讨论(0)
提交回复
热议问题