Here's how it works: For each scope created, a new stack frame is created on the program's stack. Each stack frame has it's own variables, function calls, etc. A
for loop is no different. Now, here's our two loops:
1 2
|
for(int X(0); ...)
for(int X(0); ...);
|
OK, now. Two stack frames have been created here: 1 for the innermost, and 1 for the outermost. Both stack frames have their own
X variable pushed onto them. Since the innermost loop is the most recent stack frame, the compiler will look into it, and will see
X there, and it'll use that. It won't search any further, because what's the point is searching for something that you've found? I guess you could rename the innermost
X to disambiguate it.
jumper007 wrote: |
---|
But what would be the difference if instead of i++ i would write ++i (in a for loop) ? (sic) |
There's no difference unless the expression has more than two operands/sub-expressions, like so:
for(int X(0); ... ; ((++X) + 1))
The above expression within the increment section is when the post-fix/pre-fix ++/-- matters.
Wazzak