FIX: Logical IF in GOTO Loop Produces Unexpected Results (49731)



The information in this article applies to:

  • Microsoft FORTRAN Compiler for MS-DOS 4.0
  • Microsoft FORTRAN Compiler for MS-DOS 4.1
  • Microsoft FORTRAN Compiler for MS-DOS 5.0
  • Microsoft FORTRAN Compiler for MS-DOS 5.1
  • Microsoft FORTRAN compiler for OS/2 4.1
  • Microsoft FORTRAN compiler for OS/2 5.0
  • Microsoft FORTRAN compiler for OS/2 5.1

This article was previously published under Q49731

SYMPTOMS

In an application compiled with Microsoft FORTRAN, a logical IF statement in a GOTO loop produces incorrect results.

CAUSE

The compiler incorrectly optimized the IF statement in the GOTO loop.

RESOLUTION

To address this problem, perform one of the following two steps:

  • Specify the -Od compiler option switch to disable optimizations.
  • If optimization is required, modify the source code to change the logical IF statement to a standard IF statement. For example, change the following code:
          if (j .GE. 3) GOTO 3100    ! logical IF statement
    						
    to the following code:
          if (j .GE. 3) then         ! standard IF statement
             GOTO 3100
          endif

STATUS

Microsoft has confirmed this to be a problem in FORTRAN versions 4.0, 4.1, 5.0, and 5.1. This problem was corrected in FORTRAN PowerStation, version 1.0.

MORE INFORMATION

As shown in the code sample below, the logical IF and GOTO loop compiles and runs without errors but it does not execute the correct lines of code. Because the GOTO 3100 statement in the logical IF statement is ignored, the program prints "at 3200, loop failed."

To address this problem, compile the application without optimizations. The logical IF statement works correctly and the program prints "at 3100, loop worked".

Sample Code

C Compile options needed: See above

      J = 0
3190  CONTINUE
      IF (J .GE. 3) GOTO 3100       ! The logical IF
      J = J + 1
      GOTO 3190                     ! The Goto loop
3200  CONTINUE
      WRITE (*, *) 'at 3200, loop failed'
      STOP
3100  CONTINUE
      WRITE (*, *) 'at 3100, loop worked'
      STOP
      END
				

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kbfix KB49731 kbAudDeveloper