LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

For loop in a while loop

Solved!
Go to solution

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 😞

0 Kudos
Message 1 of 14
(2,035 Views)

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.)

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 2 of 14
(2,029 Views)

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

0 Kudos
Message 3 of 14
(2,019 Views)

@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?

0 Kudos
Message 4 of 14
(2,012 Views)

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.

0 Kudos
Message 5 of 14
(2,008 Views)
Solution
Accepted by AymanB95

@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.

0 Kudos
Message 6 of 14
(2,002 Views)

That worked!! thank you so much!!

0 Kudos
Message 7 of 14
(1,997 Views)

@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.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 8 of 14
(1,956 Views)

@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.

0 Kudos
Message 9 of 14
(1,949 Views)

Since a FOR loop that iterates zero times basically does not do anything new, here's another silly solution; 🙂

 

altenbach_0-1648657964744.png

 

(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.

 

  • Is the while loop a toplevel while loop?
  • What is the purpose of the FOR loop?
  • How does it relate to the overall user experience when running the program?
  • ...

 

0 Kudos
Message 10 of 14
(1,940 Views)