# \`for i = 1:M, j = 1:N\` discrepancy (loop vs comprehension/generator)

**URL:** <https://discourse.julialang.org/t/for-i-1-m-j-1-n-discrepancy-loop-vs-comprehension-generator/75659>\
**Category:** Internals & Design\
**Tags:** syntax, loops\
**Created:** [February 2, 2022, 2:16pm UTC](https://discourse.julialang.org/t/for-i-1-m-j-1-n-discrepancy-loop-vs-comprehension-generator/75659 "2022-02-02T14:16:23Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [February 4, 2022, 1:43pm UTC](https://discourse.julialang.org/t/for-i-1-m-j-1-n-discrepancy-loop-vs-comprehension-generator/75659/14 "2022-02-04T13:43:35Z")

</div>

The purpose of the `for i, j` form is to create a shaped array (2D in this case) and the `i, j` syntax is consistent with the array indexing syntax: first rows then columns. So the number of elements for `j` cannot depend on `i`. This syntax is inherently more restrictive than `for ... for ...`.

The order of iterations in `for i, j` is consistent with the “right order” of iteration for most arrays: since Julia arrays are column-major by default, it’s generally best to iterate first on the row index (i.e. along a column). In particular, the `Array` created by array comprehension is indeed column-major, so using this order means Julia can fill the new array in the most efficient way. But as @mcabbott says it’s probably best to use this syntax only when the order doesn’t matter anyway.

For me the questionable choice is to allow `for i, j` outside of comprehensions/generators. It’s confusing indeed that the order is not the same as in a comprehension, and I wish the syntax was simply not allowed. But once you decide to allow it, it makes sense to use the lexical order of iteration because in this case `i` and `j` have no defined association with rows and columns, and the reverse-lexical order would be even more confusing…

---

_[View the full topic](https://discourse.julialang.org/t/for-i-1-m-j-1-n-discrepancy-loop-vs-comprehension-generator/75659)._
