Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.csd.uwm.edu!leah!rpi!image.soe.clarkson.edu!news From: cline@sun.soe.clarkson.edu (Marshall Cline) Newsgroups: comp.lang.c++ Subject: Re: scope of FOR Message-ID: Date: 22 Aug 89 18:12:04 GMT References: <1807@hcr.UUCP> Sender: news@sun.soe.clarkson.edu Reply-To: cline@sun.soe.clarkson.edu (Marshall Cline) Organization: Clarkson University, Postdam NY Lines: 67 In-reply-to: ron@hcr.UUCP's message of 21 Aug 89 18:59:16 GMT In article <1807@hcr.UUCP> ron@hcr.UUCP (Ron Irvine) writes: >The expansion of the FOR statement in C++ is great. >Being able to say "for(int i=0; i<100; ++i) ... " is >just what I want when I am coding a loop. However, I >could not believe that C++ has defined the scope of the >declared name (i in this case) to be "to the end of the >block enclosing the for-statement" (pg. 294, of The C++ >programming language). It's not as pretty, but an extra pair of braces would be a patch. Ex: {for (int i = 0; i < 100; ++i) { loop statements; }} >... >The other problem is that it does not fit with the data >hiding philosophy of C++. The for statement: > for (statement-1 expression-1 ; expression-2) > statement-2 (I'm sure you realize that "statement-1" is restricted to be an "expression" statement -- I assume the apparently missing ";" is due to the fact that the "statement" contains it) >should be redefined to be the equivalent of the following >COMPOUND statement: > { /* <-- note the enclosing scope */ > statement-1 > while (expression-1) { > statement-2 > expression-2; > } > } Not quite. A minor point perhaps, but worth mentioning: If "statement-2" contains a "continue", the "expression-2" is still evaluated in the "for", but not in your "equivalent while". Simple code translators from "for" into "while" are dangerous if they fail to check for "continue"s. The only way to deal with them appears to be to translate them into a "goto" to a label just before the "expression-2". Furthermore your extra pair of "{...}" can cause some additional problems with automated code translations. For example: if (bool1) if (bool2) for (expr1; expr2; expr3) simple_statement; else some_other_statement; Your extra pair of "{...}" will cause the "else some_other_statement;" to mate with "if(bool1)" rather than with "if(bool2)" -- very bad news. Not a flame. I liked Ron's basic idea (although I'm one of the guys whose code the change would deep-6 since I occasionally use a "break" and then check the value of the loop var after I exit the loop). Marshall -- __________________________________________________________________ Marshall P. Cline Internet: cline@sun.soe.clarkson.edu ECE Department Usenet: uunet!sun.soe.clarkson.edu!cline Clarkson University Bitnet: BH0W@CLUTX Potsdam, NY 13676 AT&T: 315-268-6591