weird FOR LOOP problem (solved)

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

    weird FOR LOOP problem (solved)

    Hi, thanks for everyone's help and flames. I think I solved the prob
    but was surprised that no one noticed.

    Problem:
    Given this expression, a, b never increase under my compiler.

    double answer=0.0;
    for( int i=0, double a=0.0, double b=0.0 ; i<100 ; i++, a+=0.1, b+=0.1
    )
    answer += i+a+b;

    Reason:
    because of the required comma separator in the for loop, it is
    syntactiacally incorrect to declare both int and double in the for
    loop. (though my compiler compiled anyway...)

    This works:
    double answer=0.0;
    int i=0;
    for(double a=0.0, b=0.0 ; ... ; ...)
    ....

    The moral? Be careful of abbreviative shortcuts.
  • Mike Wahler

    #2
    Re: weird FOR LOOP problem (solved)


    Bo <snarlly@hotmai l.com> wrote in message
    news:4f190004.0 308211405.6ac78 290@posting.goo gle.com...[color=blue]
    > Hi, thanks for everyone's help and flames. I think I solved the prob
    > but was surprised that no one noticed.
    >
    > Problem:
    > Given this expression, a, b never increase under my compiler.
    >
    > double answer=0.0;
    > for( int i=0, double a=0.0, double b=0.0 ; i<100 ; i++, a+=0.1, b+=0.1
    > )
    > answer += i+a+b;
    >
    > Reason:
    > because of the required comma separator in the for loop, it is
    > syntactiacally incorrect to declare both int and double in the for
    > loop. (though my compiler compiled anyway...)
    >
    > This works:
    > double answer=0.0;
    > int i=0;
    > for(double a=0.0, b=0.0 ; ... ; ...)
    > ...
    >
    > The moral? Be careful of abbreviative shortcuts.[/color]

    Better moral: Observe the syntax (and other) rules
    of the language.

    -Mike



    Comment

    • John Harrison

      #3
      Re: weird FOR LOOP problem (solved)


      "Bo" <snarlly@hotmai l.com> wrote in message
      news:4f190004.0 308211405.6ac78 290@posting.goo gle.com...[color=blue]
      > Hi, thanks for everyone's help and flames. I think I solved the prob
      > but was surprised that no one noticed.
      >[/color]

      At least one person (Stuart Godoletz) did notice.

      But you did seem to get a totally unjustified number of flames for a
      perfectly good question.

      john


      Comment

      Working...