LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

stopping while loop

HI

 

  I am trying to stop a while loop after the last element of an array, but the problem is that the last element of the array is subject to change even when the program is running.  Any ideas?

0 Kudos
Message 1 of 8
(4,079 Views)

Just an idea: Use an autoindexing FOR loop instead of the WhileLoop…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 8
(4,069 Views)

You can have any combination of logical comparisons to figure out if it's the last element in the array...

But why don't you just use a For loop? If a For loop isn't what you're looking for then there's probably a better way to do what you're trying to do. Sharing your code is the best way for us to help.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 3 of 8
(4,065 Views)

Hi fg,

 

this seems related to your old thread, so please don't create double posts and stick with your other thread!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 8
(4,055 Views)

What exactly does that mean?

 

Does that mean the value of the last element will change?  Or which element is last may change (the array gets longer or shorter as you process it)?

 

If the array is changing while it is running, what is causing it to change?  Code inside the loop, or other code running in parallel external to the loop?

 

 

0 Kudos
Message 5 of 8
(4,051 Views)

Sorry about creating a double post on this issue.  I cannot use a for loop because the code that is suppossed to stop the while loop, along with the array and everything is inside the while looP.  If I use a For loop, I do not know how I would index it.  Also to respond to what someone said, the array might get longer or shorter if the user who is running the measurement decides to change the number of elements in the array, so the change in length of the array is managed by a user.

 

Thanks

0 Kudos
Message 6 of 8
(4,014 Views)

Is there a chance that the user could change the size/contents of the array while you are trying to read  in the loop, or you just don't know ahead of time.

 

If it's just that you don't know ahead of time, the Auto-indexing terminal of the for-loop will take care of you.  Whatever the array size is, the for-loop will run once for every loop and then stop at the end.

 

But if the array can change size/contents while you are trying to read it, that sounds like a mess.  Will they always be adding elements to the end, or could they be adding elements anywhere?  If it's anything but the end, you could miss stuff and it sounds like the whole approach sounds like it needs to be reworked.

 

But, I guess you could try compare the i terminal to the size of the array.  If your current element is ==size-1, then you are at the end of the array, so stop the loop.  This is assuming you are using the i terminal of the loop to index your array.

 

I think you should try to consider some of the suggestions in the other thread.  Those guys are really good at LabVIEW, and since you posted your code in there they were able to give you pretty specialized advice.  It might sound confusing, but it's probably the advice you need.

Message 7 of 8
(3,989 Views)

Just use DataFlow.  Here I'm illustrating this using a Sequence structure -- I (almost) never use these, but would strongly recommend instead having two sub-VIs, one called "Get Next Array Element" and the second called "Check for Last Element" that contains the logic to decide if you are finished (it should have as inputs any and all information it needs to decide if it is time to stop).  In this trivial example, the "stop" decision is set to "True".  The important point is that you (probably) need to delay the stop decision until after you take what may become the last Array Element -- DataFlow allows you to do that.

Data Flow.png

Bob Schor

0 Kudos
Message 8 of 8
(3,980 Views)