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
}
system
October 31, 2014, 5:24pm
2
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
Robin2
October 31, 2014, 5:28pm
3
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
system
October 31, 2014, 5:40pm
4
In isolation it may look a little odd, but I can't agree it is obscure or obfuscated.
It's not unheard of...
In the C and C++ programming languages, the comma operator (represented by the token ,) is a binary operator that evaluates its first operand and discards the result, and then evaluates the second operand and returns this value (and type); there is a sequence point between these evaluations.
The use of the comma token as an operator is distinct from its use in function calls and definitions, variable declarations, enum declarations, and similar constructs, where it acts as a separator.
The comm...
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 ?
KenF
October 31, 2014, 5:59pm
7
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;
}
Robin2
October 31, 2014, 7:54pm
9
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
...R
system
October 31, 2014, 8:47pm
10
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.