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. Advertisements

  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. Advertisements

  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. Advertisements

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:
    550
    John Saunders
    Jun 8, 2004
  2. Bo
    Replies:
    14
    Views:
    777
    Stuart Golodetz
    Aug 22, 2003
  3. GeekBoy
    Replies:
    5
    Views:
    489
    GeekBoy
    Apr 11, 2007
  4. Michael Bacarella
    Replies:
    26
    Views:
    1,722
    harri
    Nov 20, 2007
  5. dorayme
    Replies:
    1
    Views:
    786
    richard
    Jan 21, 2011
  6. Beauregard T. Shagnasty

    Re: A Weird Appearance for a Weird Site

    Beauregard T. Shagnasty, Jan 21, 2011, in forum: HTML
    Replies:
    1
    Views:
    586
    Captain Paralytic
    Jan 21, 2011
  7. will
    Replies:
    6
    Views:
    637
    Phrogz
    Dec 27, 2006
  8. Isaac Won
    Replies:
    9
    Views:
    788
    Ulrich Eckhardt
    Mar 4, 2013
Loading...