[Solved] so I have this loop but I don’t understand what does it do so can someone tell me (int i = 0; i


Regarding the code:

for (int i = 0; i <= n; d[i, 0] = i++)
{
}

A for loop has four parts:

  • Initialisation, performed before any iterations of the loop are done. If empty, no initialisation is done;
  • Continuation condition, a condition that must be true in order to execute the body (on any iteration, incuding the first). If empty, it’s considered true;
  • Post-iteration, performed after each iteration (execution of the body). If empty, nothing is done; and
  • The body itself. An empty body does nothing.

Hence your loop does the following:

  • It creates and initialises i to zero. Because it’s created here, the lifetime of the variable is limited to the loop;
  • It executes the body (empty, so nothing is done) as long as i is not greater than n;
  • For each value of i, it sets d[i, 0] to i and increments i as a side effect.

In other words, it sets d[i, 0] to i for values of i between 0 and n inclusive.


As an aside, because I value seeing as much code as possible on my screen, I tend to prefer something like:

for (int i = 0; i <= n; d[i, 0] = i++) {}

But your coding guidelines may not allow for this, at which point I would probably lobby to change the guidelines 🙂

solved so I have this loop but I don’t understand what does it do so can someone tell me (int i = 0; i <= n; d[i, 0] = i++)