# For Loop Style Question

**URL:** <https://discourse.julialang.org/t/for-loop-style-question/40271>\
**Category:** New to Julia\
**Tags:** loops\
**Created:** [May 27, 2020, 4:09pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271 "2020-05-27T16:09:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 27, 2020, 4:09pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271/1 "2020-05-27T16:09:21Z")

</div>

Given that `range1`, `range2`, `f()`, and `g()` are set beforehand, what is the preferred way to populate a matrix if we need both indices and values?

```julia
A=zeros(length(range1),length(range2))
for i in 1:length(range1)-1, j in 1:length(range2)
    A[i+1,j]=A[i,j]+f(range1[i])+g(range2[j])
end

```

```julia
A=zeros(length(range1),length(range2))
for (i,value1) in enumerate(range1[1:end-1]), (j,value2) in enumerate(range2)
    A[i+1,j]=A[i,j]+f(value1)+g(value2)
end

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 27, 2020, 4:18pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271/2 "2020-05-27T16:18:04Z")

</div>

The first seems simpler to me, assuming both of your `range` arrays use 1-based indexing. Note that `range1[1:end-1]` makes a copy (although if this is a `Range` object then it is still efficient because the copy is another `Range`).

Note that I would swap the loop order in order to get better locality (since `A` is stored column-major) for cache-line efficiency.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 27, 2020, 4:24pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271/3 "2020-05-27T16:24:14Z")

</div>

Isn’t 1-based indexing required by Julia? Thanks for you comments. I wasn’t sure how the loops were parsed when on a single line.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 27, 2020, 4:27pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271/4 "2020-05-27T16:27:03Z")

</div>

Not at all! Julia `Arrays` have 1 based indexing, but `AbstractArrays` can have any indexing behavior. For a particularly funny example, [Random-based indexing for arrays](https://giordano.github.io/blog/2020-05-23-random-array-indexing/)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 27, 2020, 4:31pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271/5 "2020-05-27T16:31:58Z")

</div>

> [@Nathan\_Boyer](#):
>
> `A[i+1,j]=A[i,j]+f(range1[i])+g(range2[j])`

Here you are calculating `f(range1[i])` over and over, once for each iteration over `range2`. You should probably pull this out of the inner loop.

And the first loop example is definitely cleaner.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [May 27, 2020, 4:40pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271/6 "2020-05-27T16:40:09Z")

</div>

I liked the first way better too. It’s pretty much exactly how I would write the code in Matlab. I wanted to make sure I wasn’t missing some Julian efficiency or style.

Why would I want to use enumerate and create a new variable instead of just calling the original range with an index?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 27, 2020, 4:47pm UTC](https://discourse.julialang.org/t/for-loop-style-question/40271/7 "2020-05-27T16:47:10Z")

</div>

> [@Nathan\_Boyer](#):
>
> Why would I want to use enumerate and create a new variable instead of just calling the original range with an index?

If you want to handle more general objects, e.g. user-defined array types with non 1-based indexing, or even iterable objects that are not indexable at all.

Basically, a little more thought is required to write code that is more generic. But it is perfectly fine to write less-generic, obvious code to start with, and genericize it later as needed (e.g. if you put it into a library that you start re-using in lots of new contexts).

Also, I second @DNF’s suggestion that you may want to precompute `frange1, grange2 = f.(range1), g.(range2)` so that you only evaluate these functions once per range element (_if_ performance matters and these functions are expensive).
