C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

C++ Scope

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jonathan Clements
Guest





PostPosted: Sun Jun 29, 2003 6:26 pm    Post subject: C++ Scope Reply with quote



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.




Back to top
Kurt Krueckeberg
Guest





PostPosted: Sun Jun 29, 2003 6:30 pm    Post subject: Re: C++ Scope Reply with quote




Quote:
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.




Back to top
Stuart Golodetz
Guest





PostPosted: Sun Jun 29, 2003 8:08 pm    Post subject: Re: C++ Scope Reply with quote



"Jonathan Clements" <jonathan (AT) psilocybe (DOT) demon.co.uk> wrote

Quote:
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.

Quote:
Thanks,

Jon.



Back to top
Jonathan Clements
Guest





PostPosted: Sun Jun 29, 2003 8:30 pm    Post subject: Re: C++ Scope Reply with quote

Thank you to Kurt K., John H., and Stuart G. for your replies...

"Jonathan Clements" <jonathan (AT) psilocybe (DOT) demon.co.uk> wrote

Quote:
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.







Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.