I have several questions.
In C, AFAIU, a for loop is just syntactic sugar for a while loop.
for (i1; i2; i3) i4;
is equivalent to
i1
while (i2) {
i4;
i3;
}
Now, assume I have the following:
for (i=0; i<foo(); ++i) bar();
If I understand correctly, foo() will be evaluated for every
iteration of the loop, EVEN IF the result does not depend on i?
If I recall correctly, the termination condition is evaluated only
once in some languages such as Pascal.
Is it my responsability to do:
n = foo();
for (i=0; i<n; ++i) bar();
to tell the compiler NOT TO evaluate foo() every iteration since I
know it is invariant?
Do optimizing compilers figure it out by themselves?
What if the termination condition is:
for (i=0; i<p->n; ++i) bar();
Will I similarly pay for one pointer dereferencing every iteration
and should I write instead:
n = p->n;
for (i=0; i<n; ++i) bar();
Thanks to all who care to reply!