Question about For Next loop. Need help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • t4zone58
    New Member
    • Oct 2006
    • 2

    #1

    Question about For Next loop. Need help.

    Hi
    Basically I am print out a group of number with a certain increment. However I found out a wierd thing happened from the result
    The code I wrote:

    Dim number1, number2, counter, increment As Double

    Console.Write(" Enter number1:")
    number1 = Console.ReadLin e
    Console.Write(" Enter number2:")
    number2= Console.ReadLin e
    Console.Write(" Enter increment: ")
    increment = Console.ReadLin e For count = lower_flow_rate To upper_flow_rate Step increment
    count = count + increment
    Console.WriteLi ne("{0:F}", count)
    next
    I was trying to print out a group of number from 0.8-1.5 with the increment of 0.05. However when the result came out it's 0.85,0.95, 1.05, 1.15,1.25, 135.1.45. and the increment is not 0.05 but 0.1.
    But when i try any number that is great than 1, it works. Fox example number from 1.0-1.5, all the numbers that are printed out are with the 0.05 increment.
    I got really confused here.
    can anyone plz help me with this

    Really appreciate!!!
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Just a quick one to think about - your DIM statement may not be producing exactly what you expect. Keeping in mind that I don't know what version of BASIC you're working with, in my one (VB6) this statement:
    Code:
    Dim number1, number2, counter, increment As Double
    would create increment as a Double, and the rest as whatever the default type is (probably Variant). Perhaps you could try this, just to see whether it makes any difference:
    Code:
    Dim number1 As Double, number2 As Double, counter As Double, increment As Double
    This is just a general tip - make sure you know what your code is doing, before you decide it shouldn't be doing it. :)

    I have no idea whether this will help in your specific case.

    Comment

    Working...