Topic: declarations in for loops
Author: steveha@microsoft.UUCP (Steve Hastings)
Date: 1 Aug 90 18:11:50 GMT Raw View
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 ` \\==|
Author: shap@thebeach.wpd.sgi.com (Jonathan Shapiro)
Date: 2 Aug 90 18:03:20 GMT Raw View
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