KnowledgeBase Archive

An Archive of Early Microsoft KnowledgeBase Articles

View on GitHub

Q37646: FOR…NEXT with 32767 or -32768 Index Fails to Stop Unless /d

Article: Q37646
Product(s): See article
Version(s): 4.00 4.00b 4.50
Operating System(s): MS-DOS
Keyword(s): ENDUSER | | mspl13_basic
Last Modified: 21-DEC-1989

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.

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

THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Copyright Microsoft Corporation 1986-2002.