The following while loop does not terminate. This is because the variable x
is being re-declared inside the while loop. But I don't understand why in the second iteration onward, the statements x<10
and y=x
considers the x defined in the outer scope and not the x
defined in the block scope in the following statement.
Is this because once the first iteration ends, the x
defined in the block scope is destroyed and the loop begins to execute fresh?
#include<iostream>
int main () {
int x = 0, y;
while(x <10 ){
y = x;
std::cout<<"y is :"<< y <<std::endl;
int x = y + 1;
std::cout<<"x is :"<< x <<std::endl;
}
std::cout<<"While loop is over"<<std::endl;
}