Path: utzoo!attcan!uunet!crdgw1!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!swrinde!ucsd!ucbvax!bloom-beacon!mintaka!spdcc!ima!mirror!howtek!cory From: cory@howtek.UUCP (Cory Kempf) Newsgroups: comp.lang.c++ Subject: Re: FORever Message-ID: <1537@howtek.UUCP> Date: 3 Oct 90 15:12:35 GMT References: <1990Sep27.150948.9109@sco.COM> Reply-To: cory@howtek.UUCP (Cory Kempf) Organization: Howtek Inc., Hudson, NH Lines: 46 In article <1990Sep27.150948.9109@sco.COM> ron@sco.COM (Ron Irvine) writes: > >BEWARE there is a major problem with C++'s "for" statement. No, the for statement works as it should... >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! hardly. see below. I will try to refrain from comment about poor programming style. >The program loops forever?!! Of course it loops forever! That is what you told it to do. Think about how that structure would be implimented in C: main() { int i; for(i=10;i<13;) { int i; // hides previous i for (i=0; i<3; ++i) ; // stuff ++i; // note the scope of i } } The result is, I believe, obvious. >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. This is a documented behaviour by the way. +C