Looping through Array but start at later Index. ForLoop vs ForEachLoop

Hi,

I want to loop through my Array but I want to start at a later Index.
My solutions, either go with a Forloop and define the start index, or use ForEachLoop and check each entry with a “Skipp me”? branch.
Any real difference here with those 2 options? One more expensive than the other? You have a better idea for this?

Thanks!

1 Like

second looks more inefficient as you will always loop thru the whole array.

First one looks like the way to go! :slight_smile:

3 Likes

Agreed.

Both versions are essentially same code. That get array element is also inside foreach loop. And you have additional condition there in foreach version of code. So extra check compared to top version.

However unless difference is for eg going trough 1000 items in for each vs doing only 900 to 1000 in first version i would pick what is more readable for me. Assuming here those loops are not nested inside other bigger loop.

But for me first version is more readable and it is faster.

3 Likes

To answer your question, the first option is better.

Though, you can modify the for each loop to have a start index (make sure you duplicate it first to not affect the original):


2 Likes

That get array element is also inside foreach loop.

That was part of my concerns that lead to my question.
Wonderful, thanks to you all for clearing this up. Appreciated!

2 Likes