Path: utzoo!attcan!ncrcan!hcr!ron From: ron@hcr.UUCP (Ron Irvine) Newsgroups: comp.lang.c++ Subject: scope of FOR Message-ID: <1807@hcr.UUCP> Date: 21 Aug 89 18:59:16 GMT Reply-To: ron@hcrvx1.UUCP (Ron Irvine) Organization: HCR Corporation, Toronto Lines: 40 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). One problem with the current definition is that it reduces the possibility of optimizations since the variable may be used after the for statement. It also makes the job of detecting simple loops much harder since the declared variable must be tracked until the end of the enclosing scope. This in turn makes it less likely that a machine looping instruction will be generated by the compiler. 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 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; } } This would let programmers localize the extent of the declared variable to the FOR statement. Bjarne could you please fix this. Cycles for nothing, | Ron Irvine, HCR Mips for free.