03-30-2022 08:59 AM
Hello Everyone,
i have a for loop inside a big while loop, when i start my VI, the for loop keeps running even though i put it to run just twice. How can i solve this? i just want that the for loop runs twice inside the while loop 😞
Solved! Go to Solution.
03-30-2022 09:10 AM
How do you know if it's doing that, or the WHILE loop just keeps looping and it just looks like your FOR loop is running more than two times per WHILE loop iteration? (Hint: click the "lightbulb" (highlight execution) button to slow things down so you can see exactly what is happening.)
03-30-2022 09:16 AM
It is doing what is supposed to do, but its doing it continuously without stopping after N- Times, because its inside the while, so its keep running again and again
03-30-2022 09:17 AM
@AymanB95 wrote:
Hello Everyone,
i have a for loop inside a big while loop, when i start my VI, the for loop keeps running even though i put it to run just twice. How can i solve this? i just want that the for loop runs twice inside the while loop 😞
Can you attach a copy of your vi or a simplified version of the vi that exhibits the issue? Also, as Bill hinted at, how do you know that the FOR loop is continuously running?
03-30-2022 09:20 AM
I did a simple VI so i can show you what i mean, i put a for in a while, the for loop should run just for 5 times and then stop, without stopping the while.
03-30-2022 09:24 AM
@AymanB95 wrote:
It is doing what is supposed to do, but its doing it continuously without stopping after N- Times, because its inside the while, so its keep running again and again
So what you're saying is that it's running and then running again on the next iteration of the while loop, etc. and you only want it to run in the first iteration. Is that correct? Put a case structure around it using the index of the while loop so that it only runs when i=0.
03-30-2022 09:27 AM
That worked!! thank you so much!!
03-30-2022 11:01 AM
@johntrich1971 wrote:
@AymanB95 wrote:
It is doing what is supposed to do, but its doing it continuously without stopping after N- Times, because its inside the while, so its keep running again and again
So what you're saying is that it's running and then running again on the next iteration of the while loop, etc. and you only want it to run in the first iteration. Is that correct? Put a case structure around it using the index of the while loop so that it only runs when i=0.
A better solution would be to move the For loop outside of the While loop. If it is truly only be used to initialize it for the first time why burden the While loop with checking a condition which will always be false except for the first time. Do the initialization outside of the While loop and then enter it.
03-30-2022 11:15 AM
@Mark_Yedinak wrote:
@johntrich1971 wrote:
@AymanB95 wrote:
It is doing what is supposed to do, but its doing it continuously without stopping after N- Times, because its inside the while, so its keep running again and again
So what you're saying is that it's running and then running again on the next iteration of the while loop, etc. and you only want it to run in the first iteration. Is that correct? Put a case structure around it using the index of the while loop so that it only runs when i=0.
A better solution would be to move the For loop outside of the While loop. If it is truly only be used to initialize it for the first time why burden the While loop with checking a condition which will always be false except for the first time. Do the initialization outside of the While loop and then enter it.
I would agree with this, but since we haven't seen the code wasn't sure if there was some data from the first iteration that was needed for this so I gave the simple, though likely not the best, solution.
03-30-2022 11:38 AM
Since a FOR loop that iterates zero times basically does not do anything new, here's another silly solution; 🙂
(Also, there is a +1 primitive that you don't even need if you would adjust the random range accordingly. "x+y" is not a useful label, there needs to be a way to stop the while loop. The while loop probably also needs some timing, etc.)
I agree that we would need significantly more context to find a good solution to whatever the OP is trying to
do. e.g. by looking at the real code and not some stripped down example.