for loop stop condition

D

Douglas

Hi,
In the loop

for(i=0; i< h+1; i++);

if h=10 say, then is h+1 evaluated every time or, between iterations,
does the for loop remember that the condition i<11 is being applied?
I'm guessing that it evaluates every time as it is possible that h may
have been changed by the body of the loop.

Douglas
 
X

xarax

Douglas said:
Hi,
In the loop

for(i=0; i< h+1; i++);

if h=10 say, then is h+1 evaluated every time or, between iterations,
does the for loop remember that the condition i<11 is being applied?
I'm guessing that it evaluates every time as it is possible that h may
have been changed by the body of the loop.

Douglas

The condition is fully evaluated every time. If you
want h+1 evaluated only once, then do that before
entering the loop and save it in a variable, then
use that variable for the condition.
 
J

Jens.Toerring

The condition is fully evaluated every time. If you
want h+1 evaluated only once, then do that before
entering the loop and save it in a variable, then
use that variable for the condition.

Can't a clever optimizing compiler determine, at least under some
circumstances (e.g. h ist never changed directly and isn't defined
as volatile and there are no changes done via pointers), that h can
not change it's value within the loop and than compare to 11 instead
of evaluation h+1 each time round?

At least a short look at the assembler produced by gcc for e.g.

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
int i, h;
h = 10;

for ( i = 0; i < h + 1; i++ )
printf( "%d\n", i );

return EXIT_SUCCESS;
}

looks a lot as if the comparison is done against the number 11 and not
the result of the calculation of h+1.
Regards, Jens
 
J

Jack Klein

Hi,
In the loop

for(i=0; i< h+1; i++);

if h=10 say, then is h+1 evaluated every time or, between iterations,
does the for loop remember that the condition i<11 is being applied?
I'm guessing that it evaluates every time as it is possible that h may
have been changed by the body of the loop.

Douglas

The C abstract machine requires evaluation of the condition each time,
but the "as-if" rule allows it to be optimized away if the compiler
can analyze the code and determine that it is not modified in the loop
body.

This is called "loop invariant code hoisting", and is a very common
optimization technique used by compilers.

Consider:

void blank_lines(int x)
{
int i;
for (i = 0; i < x+1; ++i)
{
putchar('\n');
}
}

Since x is automatic, is not defined as volatile, is not modified
within the loop, and its address is not passed to a function, the
compiler can tell that it cannot be modified within the loop, so it
can optimize the calculation away.

On the other hand, given:

void stranger_func(int *);

void strange_func(int x)
{
int i;
for (i = 0; i < x+1; ++i)
{
stranger_func(&x);
}
}

Here, especially if the definition of stranger_func() is another
source file, the compiler cannot assume that the value of x remains
constant throughout the loop, and must calculate x+1 each time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top