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

ID: Q87769


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0
  • Microsoft QuickBASIC for MS-DOS, versions 4.0, 4.0b, 4.5
  • Microsoft BASIC Professional Development System (PDS) for MS-DOS, versions 7.0, 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 query words: 1.00 2.00 4.00 4.00b 4.50 7.00 7.10 b_quickbas b_basiccom

Keywords :
Version : MS-DOS:4.0,4.0b,4.5; WINDOWS:1.0,2.0,3.0; :7.0,7.1
Platform : MS-DOS WINDOWS
Issue type :


Last Reviewed: January 25, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.