weird FOR LOOP problem (solved)

Discussion in 'C++' started by Bo, Aug 21, 2003.

  1. Bo

    Bo Guest

    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.
     
    Bo, Aug 21, 2003
    #1
    1. Advertising

  2. Bo

    Mike Wahler Guest

    Bo <> wrote in message
    news:...
    > 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.


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

    -Mike
     
    Mike Wahler, Aug 21, 2003
    #2
    1. Advertising

  3. "Bo" <> wrote in message
    news:...
    > Hi, thanks for everyone's help and flames. I think I solved the prob
    > but was surprised that no one noticed.
    >


    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
     
    John Harrison, Aug 22, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Scott Meddows
    Replies:
    1
    Views:
    424
    John Saunders
    Jun 8, 2004
  2. GeekBoy
    Replies:
    5
    Views:
    397
    GeekBoy
    Apr 11, 2007
  3. Michael Bacarella
    Replies:
    26
    Views:
    1,420
    harri
    Nov 20, 2007
  4. will
    Replies:
    6
    Views:
    442
    Phrogz
    Dec 27, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    458
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page