[comp.std.c++] declarations in for loops

jimad@microsoft.UUCP (Jim ADCOCK) (07/31/90)

Proposed:

That the traditional C++ ability to declare variables in for loops
be maintained, and maintain its traditional meanings, for example:

doFoo()
{
	for (int i=0; i<100; ++i)
		for (int j=0; j<i; ++j)
			doSomething(i,j);
}

*is* legal, and i's and j's scopes extend to the end of doFoo().

[disclaimer: this represents the opinions of an individual C++ user]

steveha@microsoft.UUCP (Steve Hastings) (08/02/90)

In article <56168@microsoft.UUCP> jimad@microsoft.UUCP (Jim ADCOCK) writes:
>Proposed:
>
>That the traditional C++ ability to declare variables in for loops
>be maintained, and maintain its traditional meanings, for example:

What about while(), do...while(), and if() statements?
-- 
Steve "I don't speak for Microsoft" Hastings    ===^=== :::::
uunet!microsoft!steveha  steveha@microsoft.uucp    ` \\==|

shap@thebeach.wpd.sgi.com (Jonathan Shapiro) (08/03/90)

In article <56168@microsoft.UUCP>, jimad@microsoft.UUCP (Jim ADCOCK) writes:
> Proposed:
> 
> That the traditional C++ ability to declare variables in for loops
> be maintained, and maintain its traditional meanings, for example:
> 
> doFoo()
> {
> 	for (int i=0; i<100; ++i)
> 		for (int j=0; j<i; ++j)
> 			doSomething(i,j);
> }
> 
> *is* legal, and i's and j's scopes extend to the end of doFoo().
> 
> [disclaimer: this represents the opinions of an individual C++ user]

This would be wrong.  The correct scope rule is that i's and j's scopes
extend to the end of the scope in which the for() statement appears. 
Consider the example

doFoo()
{
	{
		for (int i = 0; i < 100; ++i)
			for (int j=0; j<i; ++j)
				doSomething(i,j);
		...
	} /* scope of i and j ends here */
} /* definitely NOT here */

It's been my experience that this feature causes confusion.

Jon