Unusual FOR TO Loop question...

I am looking at some code to draw filled triangles... here
.. but thats not important right now..

There is a line of code (not written for arduino), a FOR TO loop, which looks unusual

for(;S.y<=B.y; S.y++, E.y++, S.x+=dx2, E.x+=dx1)

i get the for(from; to; stepsize) bit, but is it possible to have more steps/maths inside the parenthesis ?

would this be the same as

for(S.y=0 ;S.y<=B.y; S.y++) {
  E.y++;
  S.x+=dx2;
  E.x+=dx1;
 // blah blah more code etc
}

i get the for(from; to; stepsize) bit

Then you don't get it. The for statement has an initialization part, a while clause, and an post-block section.

In the snippet you posted, the initialization section is:


The while clause is:

S.y<=B.y

And, the post-block section is:

S.y++, E.y++, S.x+=dx2, E.x+=dx1

would this be the same as

The first part can not be answered since there is no initialization code in the snippet you posted.

The blah blah blah stuff comes BEFORE the changes to E.y, S.x, and E.x

for(;S.y<=B.y; S.y++, E.y++, S.x+=dx2, E.x+=dx1)

I'm guessing that the code is designed to start at whatever value S.y already has and will keep going until S.y <= B.y And at every step it will update the values of S.y, E.y, S,x and E.x

Somebody must be doing a PhD in obscure coding. I was hoping those courses had been stopped.

...R

In isolation it may look a little odd, but I can't agree it is obscure or obfuscated.

It's not unheard of...

However, this kind of use is not recommended because, indeed, it makes the code cluttered and doesn't help much in terms of performance.

so am I allowed to use the following in Arduino ?

for( ; S.y<=B.y; S.y++) {
// blah blah code etc
  E.y++;
  S.x+=dx2;
  E.x+=dx1;
}

I am just concerned if the loop will still work uninitialised, and to what extent, like Robin said, does the loop start at what ever value S.y already is ?

Since the initialisation option has been omitted. It's equivalent to

while(S.y<=B.y)
  {
    S.y++;
    E.y++;
    S.x+=dx2;
    E.x+=dx1;   
  }

Thank you Ken

KenF:
Since the initialisation option has been omitted. It's equivalent to

And isn't that so much easier to understand without having to suck your pencil :slight_smile:

...R

And isn't that so much easier to understand without having to suck your pencil

Well, not really. What it makes me wonder is why the for loop was used in the first place. Or why, the initialization section was performed outside of the for loop.