0

can anyone explain this?

public class Program { public static void main(String[] args) { boolean b= true; for(int i=0; i<10; i++) if(b=!b) System.out.print(i); } } output: 13579

21st Feb 2018, 2:40 AM
Strawhat Ken
Strawhat Ken - avatar
2 Answers
+ 7
b = true, !b = false because we declared b as true. we said b = !b. b is now becoming false and because of that it always by pass by 2 (i = 0 then b is false, i = 1 value is changed to true, i = 2 b is false again etc) because in every new intereation they change value. b must be true to get inside of if
21st Feb 2018, 3:06 AM
Dejan Perovic
Dejan Perovic - avatar
0
thanks
21st Feb 2018, 5:23 AM
Strawhat Ken
Strawhat Ken - avatar