for() Loop Scoping
 
 
 

We have upgraded all for() loops to follow the C++ standard. The following code shows an example of when for() loop scope is misunderstood:

for ( int i = 0; i < 10; ++i ) { /* stuff... */ }
for ( i = 0; i < 20; ++i ) { /* more stuff, including a break */ }
return (i == 20) ? this_stuff : that_stuff;

Since i is declared inside the first for() loop, it goes out of scope when the loop completes, causing the second for() loop to fail to compile. The same is true for the return statement.

Generally, with code that was simple enough, we modified the previous code as follows:

for( int i = 0; i < 10; ++i ) { /* stuff... */ }
for( int i = 0; i < 20; ++i ) { /* if break condition met, return this_stuff */ }
return that_stuff;

To keep the impact of the changes as small as possible, non-trivial for() loops were changed as follows:

int i;
for( i = 0; .... )

3ds Max SDK users are encouraged to follow the C++ convention for keeping the scope of for() loop counters within the loops.