From 04:00 PM CDT – 08:00 PM CDT (09:00 PM UTC – 01:00 AM UTC) Tuesday, April 16, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

For Loop Problem

I have a for loop that seems to be giving me inconsistant results.  It doesn't seem to execute the correct number of times.  Here is my code:
    a = Min
    CRange = ((Max - Min) * 10) + 1
    For x = 1 To CRange
      ChD(x, PhaseRange) = a
      a = a + 0.1
    Next
Min and Max are values that contain up to one decimal place.  Given a certain set of data I had a Min of 0.8 and a Max of 1.  Now the purpose of this loop is to count the difference between Min and Max in 0.1 increments.  So for the values listed I would have 0.8, 0.9, and 1.0.  The statement CRange = ((Max - Min) * 10) + 1 should return 3 and it does.  I've confirmed this with the debugger.  Now I would expect the For Loop to cycle through 3 times but it doesn't.  It only executes twice and then carries on.  I thought maybe it had something to do with Min and Max being floats so I converted CRange into an integer using Int() but that didn't help either.  What is really boggling me is that for a different set of data with a different range (0.2-1.8) the Loop executes the correct number of times (17). 

Can anyone shed some light onto this?  I'm at my wits end. 
0 Kudos
Message 1 of 3
(3,153 Views)
Hello Pat_Cas!

You was right the problem is that CRange is a float. The command int() only cut off the decimal places but the result is still a float. If you use the command trunc() instead the loop will work.

Matthias
Matthias Alleweldt
Project Engineer / Projektingenieur
Twigeater?  
0 Kudos
Message 2 of 3
(3,140 Views)
Ah, you have saved my sanity. 
You sir, are a gentleman and a scholar. 
0 Kudos
Message 3 of 3
(3,133 Views)