Re: problems with the scoping in eu4
- Posted by mattlewis (admin) Jan 07, 2011
- 2038 views
I don't think that the complaint is persistence outside of the loop. After all, that doesn't even make sense.
I think that the complaint is persistence from iteration to iteration. The variable is either unassigned, or it is reassigned some default value on every new iteration of the loop.
The simple example initially given was a good one, trying to use two counters in one loop.
In that case, it's simply misunderstanding how lexical scopes generally work. For example:
#include <stdio.h> int main(){ int i = 5; while( i ){ int j = 0; printf("--i: %d ++j: %d\n", --i, ++j ); } return 0; }
Here's what happens:
$ gcc -oloop loop.c && ./loop --i: 4 ++j: 1 --i: 3 ++j: 1 --i: 2 ++j: 1 --i: 1 ++j: 1 --i: 0 ++j: 1
A variable goes out of scope when the end of its scope is reached. In this case, the scope is the body of the loop. The original request seemed to me to be for the equivalent of C's static (function-level) variables inside a particular scope.
Matt