Path: utzoo!attcan!uunet!convex!graham From: graham@convex.com (Marv Graham) Newsgroups: comp.lang.c++ Subject: Re: FORever Message-ID: <106731@convex.convex.com> Date: 1 Oct 90 19:30:06 GMT References: <1990Sep27.150948.9109@sco.COM> Sender: usenet@convex.com Organization: Convex Computer Corporation; Richardson, TX Lines: 60 In article <1990Sep27.150948.9109@sco.COM> ron@sco.COM (Ron Irvine) writes: > >BEWARE there is a major problem with C++'s "for" statement. > >main() { > for (int i=10; i<13;) { > for (int i=0; i<3; ++i) > ; // stuff > ++i; > } >} > >Cfront accepts this code and compiles without warning. >The resulting code is totally unexpected! >The program loops forever?!! > >I realize the problem is that cfront does not end the >scope of the inners loop's "i" until the end of the >outer loop, thus the "++i;" statement actually increments >the inner loops "i" whos scope is the outer loop. And I >did not "redeclare" "i" in the outer loop since the outer >"i"'s scope is outside the outer loop. > >This is a bad joke for a language that is designed for the 90's. >The scope of a variable in a "for-init-statement" MUST end >at the end of the "for" statement. Lets change the definition of >the language NOW before we inflict horrendous pain on C++ programmers. In my opinion, the PROBLEM is using a badly defined new toy instead of writing main() { int i ; // this really belongs here for (i=10; i<13;) { { // this really is needed here int i ; // and this really belongs here for (i=0; i<3; ++i) ; // stuff } // this makes the next ++i refer to the outer one ++i; } } If you say what you mean, you get what you want. What we should really do is make the nested definitions illegal (again)! But, if you had just a little common sense, you would write main() { int i ; for (i=10; i<13;) { int j ; // what, not "i" again?? for (j=0; j<3; ++j) ; // stuff ++i; } } Can't you really think of a name for a for loop index besides "i"? Marv Graham; Convex Computer Corp. {uunet,sun,uiucdcs,allegra}!convex!graham