C++ Scope

Discussion in 'C++' started by Jonathan Clements, Jun 29, 2003.

  1. Hi all,

    Does 'for' have it's own scope? In one compiler I'm quite happy
    writing:-

    for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ } and
    then (not nested)
    for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }

    Probably bad practice, but as far as I understand C++, 'i' should be a
    temp within the scope of the for loop. Some compilers allow this, others
    don't.... I'm wondering if I'm mis-understanding the scope rules or the
    compilers are.

    Thanks,

    Jon.
    Jonathan Clements, Jun 29, 2003
    #1
    1. Advertising


  2. > Hi all,
    >
    > Does 'for' have it's own scope? In one compiler I'm quite happy
    > writing:-
    >
    > for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ }

    and
    > then (not nested)
    > for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }
    >
    > Probably bad practice, but as far as I understand C++, 'i' should be a
    > temp within the scope of the for loop. Some compilers allow this, others
    > don't.... I'm wondering if I'm mis-understanding the scope rules or the
    > compilers are.
    >

    Your code is correct, even if some compilers incorrectly complain.
    Kurt Krueckeberg, Jun 29, 2003
    #2
    1. Advertising

  3. "Jonathan Clements" <> wrote in message
    news:bdnanh$lv7$1$...
    > Hi all,
    >
    > Does 'for' have it's own scope? In one compiler I'm quite happy
    > writing:-
    >
    > for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ }

    and
    > then (not nested)
    > for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }
    >
    > Probably bad practice, but as far as I understand C++, 'i' should be a
    > temp within the scope of the for loop. Some compilers allow this, others
    > don't.... I'm wondering if I'm mis-understanding the scope rules or the
    > compilers are.


    The compilers are. It's perfectly good (and common) practice to reuse loop
    variables like i. If one of your compilers doesn't like it, just put:

    #define for if(0); else for

    at the top of each translation unit and it'll fix it.

    HTH,

    Stuart.

    > Thanks,
    >
    > Jon.
    Stuart Golodetz, Jun 29, 2003
    #3
  4. Thank you to Kurt K., John H., and Stuart G. for your replies...

    "Jonathan Clements" <> wrote in message
    news:bdnanh$lv7$1$...
    > Hi all,
    >
    > Does 'for' have it's own scope? In one compiler I'm quite happy
    > writing:-
    >
    > for(unsigned long i=1; i <= WHATEVER; i++) { /* something here */ }

    and
    > then (not nested)
    > for(unsigned long i=1; i <= WHATEVER2; i++) { /* something here2 */ }
    >
    > Probably bad practice, but as far as I understand C++, 'i' should be a
    > temp within the scope of the for loop. Some compilers allow this, others
    > don't.... I'm wondering if I'm mis-understanding the scope rules or the
    > compilers are.
    >
    > Thanks,
    >
    > Jon.
    >
    >
    >
    >
    Jonathan Clements, Jun 29, 2003
    #4
    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. Paul Opal
    Replies:
    12
    Views:
    926
    Paul Opal
    Oct 11, 2004
  2. ann
    Replies:
    13
    Views:
    646
    Patricia Shanahan
    Sep 13, 2005
  3. Steven T. Hatton
    Replies:
    9
    Views:
    458
  4. Xah Lee
    Replies:
    0
    Views:
    2,221
    Xah Lee
    Feb 26, 2009
  5. Replies:
    0
    Views:
    154
Loading...

Share This Page