public class Test1{
public static void main(String[] args){
int x = 3;
do {
int y = 1;
System.out.print(y++ + " ");
x--;
} while(x >= 0);
}
}
In above code, local variable y is in scope for the block of do while statement. This is valid for all the iterations of the loop. So why does java not throw an error "Variable Y is already defined" for subsequent iterations after the first one, as we are redeclaring the same variable in each iteration?
ydoes not last from one loop iteration to the next. (And what Jon said.)