Microsoft KB Archive/87769

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

Article ID: 87769

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q87769



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.



Steps to Reproduce Behavior in Visual Basic for Windows
 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.  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  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 query words: 1.00 2.00 4.00 4.00b 4.50 7.00 7.10 b_quickbas b_basiccom

Keywords: kbprb KB87769

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.