Skip to main content
January 26, 2013
Answered

Setting different vars in for loop?

  • January 26, 2013
  • 2 replies
  • 1453 views

Hi

I have a reasonable large loop, and I want it to loop one of two ways depending on conditions before it...

1. for (var i:int = searchHit; i< array.length; i++)

or

2. for (var i:int=array.length; i>=0; i--)

I'm wondering if there is a way to set up vars to populate the above to avoid writing out two identical oops with different conditions?

I can do this...

var _inc = "++" (or "--") depending on previous code

var _range = array.length;

for (var i:int = _range; i< array.length; i[inc])

...but I'm unsure how to put i< array.length or i>=0 into a var to insert (or if it's even possible)?

Thanks for your help.

This topic has been closed for replies.
Correct answer kglad

you could use one 'do' or 'while' loop but it's faster to do what you're currently doing.  though, it would look a little more elegant and be easier to debug and maintain if you called a function inside your loops:

if( NEXT){

for (var i:int = searchHit; i< array.length; i++) {

evalF(i);

} else {

for (i=searchHit; i>=0; i--){

evalF(i);

}

2 replies

sinious
Legend
January 26, 2013

In that situation you'd set a custom flag but still need multiple loops the rest of the code you didn't share actually needs both of those loops.

e.g.

var dataComplete:Boolean = false;

// run both loops as mentioned above just how they are (except change i if it's a nested loop)

// once complete in the final loop

dataComplete = true;

if (dataComplete) { break; }

It depends on what your data is and what you're really trying to do. Explaining it more will issue a better strategy.

edit:

Kglad! On the same minute.. haha

January 26, 2013

Hang on - I buggered it up, and should explain more what I'm trying to do...

1. for (var i:int = searchHit; i< array.length; i++)

or

2. for (var i:int=searchHit; i>=0; i--)

I have a NEXT and PREVIOUS button that searches for and selects the next or previous row that matches a keyword the user enters.

So if the get 100 rows in hitting NEXT, then hit PREVIOUS, I want it to start searching from the row it's at, not the end or the beginning

(in a prior bit of code, I searchHit++ or -- (depending on NEXT or PREVIOUS) to make sure it starts looking after or before the last hit)

The above loop set ups seem to work if I use them like:

If NEXT do this loop - for (var i:int = searchHit; i< array.length; i++) else do this loop for (var i:int=searchHit; i>=0; i--)

...with identical loops for each one.

Cheers guys

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
January 26, 2013

you could use one 'do' or 'while' loop but it's faster to do what you're currently doing.  though, it would look a little more elegant and be easier to debug and maintain if you called a function inside your loops:

if( NEXT){

for (var i:int = searchHit; i< array.length; i++) {

evalF(i);

} else {

for (i=searchHit; i>=0; i--){

evalF(i);

}

kglad
Community Expert
Community Expert
January 26, 2013

unless you can decrease the iterations by many billions, it will be probably more efficient to loop backwards and NOT test for the shortest path through your loop.