From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to make a while loop stop after "x" number of iterations?

Solved!
Go to solution

I am a new labVIEW user. I am currently using labVIEW Student Edition 2009. One practice problem I have says:

 

"Modify the general while loop so that it stops when the stop button is pushed OR when the while loop reaches a certain number of iterations specified in the front panel.

Test you VI with number of iterations = 10 "

 

I made a VI that stops after 10 iterations, but I used "Elapsed Time" and then a certain millisecond delay to get 10 loops. So anytime the number of iterations changed I woul dhave to refigure the delay. There has to be a better/easier way to do this?

 

Thanks for any help.

0 Kudos
Message 1 of 11
(9,237 Views)

In the old days, you would watch for the iteration count and stop if it exceeds a certain value.

Nowadays, simply use a FOR loop and show the conditional terminal to stop early if needed.

0 Kudos
Message 2 of 11
(9,235 Views)

Read the help on the while loop.  Specifically, the part about the i terminal

--
Tim Elsey
Certified LabVIEW Architect
0 Kudos
Message 3 of 11
(9,233 Views)

I would just use a For loop but the problem says modify a while loop so I was trying to figure it out. Its eemed easy until I started trying it haha.

 

Thanks for the help though.

0 Kudos
Message 4 of 11
(9,232 Views)

I did see that about the i terminal (I used context help inside labVIEW). I can get it to display the number of iterations but I have not been able to make it stop after just 10. I spent way too much time on this, I have to be doing something dumb. 

0 Kudos
Message 5 of 11
(9,228 Views)
Solution
Accepted by topic author Coop650

try this?

 

 

Remember that the iteration starts counting with zero, so you might need some adjustments, depending on how you are counting it.

Message 6 of 11
(9,219 Views)

If i is greater than or equal to required iterations, stop the loop.  You may need to increment i first since it starts at 0

 

Yeah, what altenbach said.

--
Tim Elsey
Certified LabVIEW Architect
Message 7 of 11
(9,218 Views)

Thanks guys. I was just working on this and came up with this one and it seems to work. I really appreciate the time you all took to respond. 

 

 

0 Kudos
Message 8 of 11
(9,203 Views)

Remember i starts at 0.  Your code runs one extra time.  When your "actual iterations" indicator says 10, it's really 11.  Take a closer look at altenbach's code.

--
Tim Elsey
Certified LabVIEW Architect
0 Kudos
Message 9 of 11
(9,197 Views)

@Coop650 wrote:

Thanks guys. I was just working on this and came up with this one and it seems to work. I really appreciate the time you all took to respond. 


Never ever do an equal comparison involving orange numerics (DBL in this case). Your desired iteration control needs to be an integer (right-click..representation..I32).

 

You should also do a "greater or equal" comparison for sanity. What if the desired iteration is set to -5, the loop would never stop in your code.

 

 

Message 10 of 11
(9,187 Views)