for loop stop condition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Douglas

    for loop stop condition

    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
  • xarax

    #2
    Re: for loop stop condition

    "Douglas" <mm2ps@yahoo.co .uk> wrote in message
    news:4cec047f.0 407100923.640f3 dd8@posting.goo gle.com...[color=blue]
    > 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[/color]

    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.


    Comment

    • Jens.Toerring@physik.fu-berlin.de

      #3
      Re: for loop stop condition

      xarax <xarax@email.co m> wrote:[color=blue]
      > "Douglas" <mm2ps@yahoo.co .uk> wrote in message
      > news:4cec047f.0 407100923.640f3 dd8@posting.goo gle.com...[color=green]
      >> 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.[/color][/color]
      [color=blue]
      > 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.[/color]

      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
      --
      \ Jens Thoms Toerring ___ Jens.Toerring@p hysik.fu-berlin.de
      \______________ ____________ http://www.toerring.de

      Comment

      • Jack Klein

        #4
        Re: for loop stop condition

        On 10 Jul 2004 10:23:21 -0700, mm2ps@yahoo.co. uk (Douglas) wrote in
        comp.lang.c:
        [color=blue]
        > 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[/color]

        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(i nt *);

        void strange_func(in t 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.

        --
        Jack Klein
        Home: http://JK-Technology.Com
        FAQs for
        comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
        comp.lang.c++ http://www.parashift.com/c++-faq-lite/
        alt.comp.lang.l earn.c-c++

        Comment

        Working...