0

Would you tell me why the output of this code "13579"? Why isn't "02468"?

boolean b = true; for(int i=0; i<10; i++){ if(b=!b) System.out.println(i); }

10th Jul 2018, 2:40 AM
Afresia
Afresia - avatar
2 Respostas
+ 2
->Iteration 1: if(b = !b) <=> if(b = !true) <=> if(b = false) => therefore wont print 0 ->Iteration 2: if(b = !b) <=> if(b = !false) <=> if(b = true) => therefore will print 1 ... and so on... 13579
10th Jul 2018, 3:13 AM
Chriptus13
Chriptus13 - avatar
+ 2
when I=0 b=!b // b=!true so this will be inverterd eg if it is true then changed to false ..false means it will be true...when there is symbol ! like this so wen it's 0 it's false then I++ now b=!false // now it will be true loop is true now so it will print 1 then I++ now I=2 now b=!true // changed to false so loops failed then I++ now I=3 now b=!false // changed to true so loops true so prints 3 like wise goes on
10th Jul 2018, 3:14 AM
C Lover