When you create a loop, like:
for ( ; ; )
{
...
}
Then everything inside the { ... } gets executed each time the loop runs. Any local scope variables (or other objects) will be created and initialized when that line of code is executed, and will be destroyed (destructors called) when the } is encountered, because that local scope variable goes out of scope at that point. In other words, that variable is in scope only for a single iteration of the loop, not for all of them! This is extremely important. This means that:
- In the first loop, your variable j is initialized to 23 each time the loop executes. It is decremented to 22 at the bottom of the loop, then destroyed, and a new j is created and initialized to 23 for the next time.
- In the other loop, int decimal is created with local (to the loop) scope, and is not the same "decimal" that you declared outside of the loop! Furthermore, it is also a new unique 'decimal' each time through the loop. If this line of code was not inside the brackets {...} then it would result in a compiler error: re-declaration of a variable. But since it is inside brackets, it is simply a new differently-scoped variable which is perfectly legal (but won't do what you want).
Both of the above mean that your code can't work.
Most of the time, you do not want or need to declare new variables inside of loops.