# Interpretation of for loop.

for every j != i in {0,....,n-1}

How to interpret this for loop? I never seen this type of definition of for loop.

Is it like - j=0 to n-1; j !=i ; ++j

• It's pseudocode, isn't it? I suppose just take it literally then? – xuq01 Jul 30 '18 at 3:52
• If i is not defined before the meaning is clearly $for\;(i,j)\;\in\{(a,b) \mid a \in \{0..n-1\} \land b \in \{0,..n-1\} \land a\neq b\}$. – Giacomo Alzetta Jul 30 '18 at 12:48

Here is the probable interpretation:

for every $j \neq i$ in $\{0,\ldots,n-1\}$:

code

should be interpreted as:

for every $j$ in $\{0,\ldots,n-1\}$:

if $j \neq i$:

code

This is similar to the way this kind of statement is interpreted in mathematical text.

It is also possible that the roles of $i$ and $j$ should be switched — this should be clear from context, and it's an ambiguity that also exists in mathematical texts.

As mentioned in Evil's answer, if neither $i$ nor $j$ has been defined, then the interpretation is probably

for every $i,j$ in $\{0,\ldots,n-1\}$:

if $i \neq j$:

code

• Upvoted, but note that "for every i, j in {0,…,n−1}" isn't 100% formal either. I think Evil's formulation of that possibility (as two nested loops) is clearer. – Quuxplusone Jul 31 '18 at 7:00
• Evil's interpretation is a guess - perhaps the loops should be switched? – Yuval Filmus Jul 31 '18 at 7:02
• Swapping the order of the inner i and outer j loops has no effect on the semantics of the program. (However, one or the other ordering may well take more advantage of cache effects if we're going to use the loop indices as indices into a 2D array. See Row-major and column-major order.) – Quuxplusone Jul 31 '18 at 16:41

There is another interpretation, both $i, j$ might be variables, in that case it is double loop:

for every $j$ in $\{0,\ldots,n-1\}$:

for every $i$ in $\{0,\ldots,n-1\}$:

if $j \neq i$:

code

Simply reading it out loud helps only if we know beforehand if $i$ is fixed variable or another counter, so there is context missing.

• An analogous mathematical notation would be $$\sum_{i,j=0\\i\neq j}^{n-1}$$ – ComFreek Jul 31 '18 at 9:05

Just read it out loud. "For every $j$ that is not equal to $i$, in $\{0, \dots, n-1\}$."

In my opinion, it would be better to have written "For every $j\in\{0, \dots, n-1\}\setminus\{i\}$."

• My guess is that $i$ is a counter, too. – Andrej Bauer Jul 30 '18 at 13:13