1

I came across some code here https://stackoverflow.com/a/2922778/2176900 that started off like this:

for (i = 0, j = nvert-1; i < nvert; j = i++) {...`

and I don't understand what it does when it has four things within the parenthesis ("i = 0", "j = nvert-1", "i < nvert" and "j = i++"). I've obviously been trying to google this, but can't seem to find any answers. I'm sure if I just knew what the things in the parenthesis was called I'd be glad.

Thanks

Community
  • 1
  • 1
Henrik
  • 673
  • 8
  • 18

3 Answers3

8

It doesn't have four things; it still has three.

  • The pre-loop statement
    i = 0, j = nvert-1

  • The iteration condition
    i < nvert

  • The post-iteration action
    j = i++

That pre-loop statement is actually probably supposed to read:
var i = 0, j = nvert-1

Remember that we can declare multiple variables in a single declaration. That's what's happening here.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • So the "j = i++" means that i increases with 1 after every iteration and that j takes on whatever value that gives i? – Henrik Jun 19 '13 at 23:40
  • @Henrik: Almost - `i++` evaluates to the value _before_ the increment, so `j` takes on the old value. This is fairly basic Javascript stuff so you should consider reading your book through again. – Lightness Races in Orbit Jun 19 '13 at 23:44
  • Gotcha. I'm just more used to seeing "j = i" just before the closing curly bracket in a for-loop. Thanks though! – Henrik Jun 19 '13 at 23:47
4

To add to the excellent answers from @LightnessRacesinOrbit and @basilikum, here is a tip I find helpful when looking at an unusual for loop like this: break it down to a more primitive form.

Any for loop:

for( initialize; condition; advance ) {
    // loop body here
}

can be translated to an equivalent while loop:

initialize;
while( condition ) {
    // loop body here
    advance;
}

So the loop you found:

for ( i = 0, j = nvert-1;  i < nvert;  j = i++ ) {
    // loop body here
}

could be written:

i = 0,  j = nvert - 1;
while( i < nvert ) {
    // loop body here
    j = i++;
}

Now we can take that and break it down to even simpler steps.

As @basilikum noted, the first line:

i = 0,  j = nvert - 1;

is the same as:

i = 0;
j = nvert - 1;

and the last line of the loop:

    j = i++;

is the same as:

    j = i;
    i = i + 1;

So putting those back into the code we end up with:

i = 0;
j = nvert - 1;
while( i < nvert ) {
    // loop body here
    j = i;
    i = i + 1;
}

That's more verbose than the original for loop, but it may be easier to think about if the original loop is confusing.

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
1

Besides the syntax, that the user Lightness Races in Orbit already explained, the reason to have such a loop is most probably to loop over an array and always access two adjacent items.

var list = [1,2,3,4],
    nvert = list.length;

for (i = 0, j = nvert-1; i < nvert; j = i++) {
    //...
}

First iteration:

i = 0; //index of first item
j = nvert-1; //indext of last item

The following iterations:

j = i;     //j becomes the old value of i
i = i + 1; //i gets incremented by one

So if you consider the array [1,2,3,4] then i and j would represent the following items for each iteration:

//first iteration
i => 1
j => 4

//second iteration
i => 2
j => 1

//third iteration
i => 3
j => 2

//fourth iteration
i => 4
j => 3
basilikum
  • 10,378
  • 5
  • 45
  • 58