Should I use Low/High for arrays?


I use a lof of dynamic arrays, and I always use this to loop through them:

for I := 0 to Length(Array) - 1 do

Open in new window

I use this a lot.

Should I be using this instead?:

for I := Low(Array) to High(Array)  do

Open in new window

I did see this around, but never thought I should look into it further.

Is it the same? Or is one better? Do you recommend one over the other?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If your arrays always start at 0 then the use of 0 will be faster than calculating the low start value.  
Low will always start at the beginning of the array and it allows you to use arrays that aren't indexed using integers.
For instance you might want to count the letter frequency in a paragraph or text file
It might make sense to use arrays that are indexed with the character  

High is probably more readable than length since length has other more intrinsic meanings than the number of elements in an array.  Again if you use an array that is indexed by something other than integers then HIGH will work better.

Even for arrays that are indexed by integers you could use a non-zero based start.
Like to track high and low temperatures you might use an array that is indexed using
    TempCount[-25 .. 100]



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Delphi_developerAuthor Commented:
Thank you, mlmcc. Yes, I have only integer indexed arrays.

So, I would use:

for I := 0 to High(vArray) do 

Open in new window

Right? I like this, because in all these years and probably 1000s of for loops, I did miss a couple of  - 1 before do  and it was not good.

I use Delphi XE7, in case there is any other, better, option to do this in this version.

If the arrays are always 0 based that will work.

If there is a chance the arrays are not 0 based use

for I := Low(vArray) to High(vArray) do

> Is it the same? Or is one better? Do you recommend one over the other?

They are the same, as long as your point is the returned values.

For clarity, I recommend using Low(Array) as it provides easy way for ignoring the initial counter for the array but paying a little bit CPU cycles extra.
High(Array) against Length, has the added value of dropping the (-1).
Delphi_developerAuthor Commented:
Thank you guys!

I will use for i := 0 to High(Array) do
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.