LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create for loop USING a while loop

Hi, I would like make a for loop inside of a while loop because I want to control the i count of the loop, and overall, more control over the loop. From what I understand, labVIEW's for loop doesn't let me change where the i count starts. It always start at i=0, and what I want to do is be able to jump to specific loop iterations and then continue from there.

 

 

Thank you.

0 Kudos
Message 1 of 16
(3,640 Views)

You can use the conditional terminal for the FOR loop.

 

 

Rodéric L
Certified LabVIEW Architect
0 Kudos
Message 2 of 16
(3,637 Views)

In this case I suggest using a shift register to keep track of the iteration count. You can initialize it with any number, you can create an elaborate logic and wire it to the conditional terminal to decide if the loop should continue, and you have complete control over the iteration number it stores.

Dániel Fülöp
Field Application Engineer (CLA, CTA)
National Instruments
0 Kudos
Message 3 of 16
(3,632 Views)

hmm, I'm not sure if what you guys suggested will solve what I'm trying to do... Is it possible to change the starting iteration count using shift registers? Lets say n=10, and instead of having i start at 0, i want to start it at 5 immediately. What I want to do is, for example, I have this code where a function will run from i=0 to i=20 with n=100, after the count reaches 20, i want to JUMP BACK to like i=2 or anywhere really. 

 

 

thanks again.

0 Kudos
Message 4 of 16
(3,600 Views)

Here are some examples of how you can use while or for loops where you can have iteration counters besides i=0, 1, 2, 3, ....

 

 

Message 5 of 16
(3,594 Views)

I suggest you use a while loop with a shift register. You may need two shift registers. One to contain a loop count and one to contain what is effectively your 'i' value. If you simply need to do something like loop 100 times and have your i value repeat values 1 through 10 (or something like that) you can use modulus division to handle the i value.



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
0 Kudos
Message 6 of 16
(3,591 Views)

i is always the number of iterations and you can not change it. What you can do is add 5 to it.

=====================
LabVIEW 2012


0 Kudos
Message 7 of 16
(3,590 Views)

@Ravens Fan wrote:

Here are some examples of how you can use while or for loops where you can have iteration counters besides i=0, 1, 2, 3, ....

 

 


This For Loop will work.

For Loop with Adjustable count.png

-----------------------------------------------------------------------------------------
Reese, (former CLAD, future CLD)

Some people call me the Space Cowboy!
Some call me the gangster of love.
Some people call me MoReese!
...I'm right here baby, right here, right here, right here at home
0 Kudos
Message 8 of 16
(3,557 Views)

That is not the same as the earlier examples.

 

If you want 10 counts and start at 0, then the stop terminal will stop after 10 times.  Just like you get when you wire 10 into the N terminal and don't have a conditional loop.

 

If your start value is larger, let's say 9, then your loop will only run 1 time because i=0 on first iteration + 1+ 9 (start value).  0 + 1 +9 = 10.

 

If your start value is larger than 10, then it will run 10 times again because on the intial iteration the result of your comparison is already greater than 10 and won't stop, and will never be equal on later iterations.

 

So your example does not even give consistent results depending on the value you use as the start value.

 

(Just to note, my earlier examples may not all be identical results as I didn't try to verify all the stop conditions nor the array that is output by the ramp function.  I could be off by one iteration here or there.  But they should be good enough to point out distinctive ways to accomplish what the OP asked for.)

0 Kudos
Message 9 of 16
(3,541 Views)

The idea was to show the OP how to control the number of iterations of the For loop.  Your example will run 10 times regardless, every time, which didn't meet the OPs requirements.

-----------------------------------------------------------------------------------------
Reese, (former CLAD, future CLD)

Some people call me the Space Cowboy!
Some call me the gangster of love.
Some people call me MoReese!
...I'm right here baby, right here, right here, right here at home
0 Kudos
Message 10 of 16
(3,523 Views)