Ankur kothari wrote:
why does this print me
1
0
3
0
The iteration is:
loop will run the length of arr;(i.e. 4)
arr{0=1, 1=2, 2=3, 3=4}
first iteration whereas: i=arr[0]=1;
System.out.println(i); // i=1;
arr[i]=0; //arr[1]=0
second iteration whereas: i=arr[1]=0; as we altered arr[1] position in first iteration;
System.out.println(i); // i=0;
arr[i]=0; //arr[0]=0
third iteration whereas: i=arr[2]=3;
System.out.println(i); // i=3;
arr[i]=0; //arr[3]=0
fourth iteration whereas: i=arr[3]=0; as we alter arr[3]=0 position in third iteration;
System.out.println(i); // i=0;
arr[3]=0; //arr[3]=0
Loop exits;
Hope its clear to you now.
Minhaj