Testing some snippets, I found a very strange behaviour. I have 2 local variables that it's not initialized. And one that it's used later in the program is always initialized to zero, the other has a garbage value (what I expected to get as a normal behavior). As if the compiler checks that I will use it in a while loop and it initialized to zero implicitly. I don't understand this behavior.
int i,j; // variable declaration and initialization
cout << i <<' '<< j << endl;
while ( i < 10 ) // condition
{ cout << i << '\n';
i++; // variable update
}
If I include the above snippet in a main program, it seems it works well always. The value of i is always zero before it enters the while loop and the while loop works well always. But the variable j has any unknown variable (garbage)
In fact,I copied this snippet directly from the book: Jumping into C++ by Alex Allain, page # 74. (I added variable j for comparison only)