PRB: For Loop w/ Integer Counter & Increment <=.5 Causes Hang

Last reviewed: June 21, 1995
Article ID: Q87769
This information applies to the following Microsoft Basic products:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft Visual Basic programming system for Windows, version 1.0 - Microsoft QuickBasic for MS-DOS, versions 4.0, 4.0b, and 4.5 - Microsoft Basic Professional Development System (PDS) for MS-DOS,
  versions 7.0 and 7.1

SYMPTOMS

If you write a FOR loop with an INTEGER or LONG variable as the FOR loop counter and use a floating point value less than or equal to 0.5 as the FOR loop increment, the loop never terminates. This causes the computer to hang (stop responding to input).

CAUSE

All Basic programs convert floating point values less than 0.5 to the integer value 0.

RESOLUTION

To stop a program that is executing in this type of an endless loop, press CTRL+BREAK.

STATUS

This behavior is by design. In other words, this is not a problem with the FOR statement; this is the way Basic is designed to operate.

MORE INFORMATION

Steps to Reproduce Behavior in Visual Basic for Windows

  1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default.

  2. Add the following code to the Form_Click event procedure for Form1:

       Sub Form_Click ()
          For j& = .005 To .0062 Step .0001
             total! = total! + j&
          Next j&
          Print total!
       End Sub
    
    

  3. Press F5 to run the example.

No value appears on the form. The program is in an endless loop. You cannot access any menus. Press CTRL+BREAK to stop the program.

To change this example program so that the loop terminates, change the type of the counter variable from LONG to SINGLE (change j& to j!).


Additional reference words: 1.00 2.00 4.00 4.00b 4.50 7.00 7.10 b_quickbas
b_basiccom
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgOptMemMgt


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.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.