+ 6

Why the output is "infinity loop"??

for (int i=0; i <5;){ i=i++; System.out.print (i); }

19th Jul 2020, 9:37 AM
K.Suhanya
K.Suhanya - avatar
2 Respuestas
+ 2
i = i++; first post increament operator does like i = i ; Next it does i = i +1 but, it will overridden by i = i result. Am not sure what exactly happens but it will not store increment result.. Its may be depending upon sequence of operation done by stack operations internally, it store a temp variable. Here, internally its happens : int temp = i; i = i+1 ; i = temp; Because of this sequence it will loss its increment result.. Just put i++; it will not an Infinite loop.. Else take another variable like j = i++; So here, int temp = i; i = i+1; j = temp; So Here, j = i happens, next i= i+1 will retain its increment value also.. But with same variable it's incrementation result will loss... Hope it helps....
19th Jul 2020, 9:48 AM
Jayakrishna 🇮🇳
0
After five time
23rd Jan 2021, 1:24 AM
Mohammed Mohammed Al-Afeef
Mohammed Mohammed Al-Afeef - avatar