FOR...NEXT with 32767 or -32768 Index Fails to Stop Unless /d (37646)






This article was previously published under Q37646

SUMMARY

If you use an integer for a FOR...NEXT loop counter, and the top of the loop is 32767 (when STEP is positive) or the bottom of the loop is -32768 (when STEP is negative), an overflow error will be correctly generated by QB.EXE or by the EXE compiled with /d. Without the /d, the loop does not stop at the top/bottom; it wraps around and executes indefinitely.

The problem occurs because overflow checking is only done when /d compiler switch is specified.

MORE INFORMATION

The following is a code example:

This program never finishes the FOR NEXT loop when compiled without /d.
DEFINT A-Z
FOR X = 1 TO 32767
  IF X MOD 100 = 0 THEN PRINT X
NEXT X
				

Modification Type: Minor Last Reviewed: 1/8/2003
Keywords: KB37646