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



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 2.0
  • Microsoft Visual Basic Standard Edition for Windows 3.0
  • Microsoft Visual Basic Professional Edition for Windows 2.0
  • Microsoft Visual Basic Professional Edition for Windows 3.0
  • Microsoft Visual Basic Standard Edition for Windows 1.0
  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 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.

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!).

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:kbprb KB87769