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
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.
Sign up to join this communityHere 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
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.)
$\endgroup$
Jul 31, 2018 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.
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\}$."
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\}$. $\endgroup$