Can anyone explain to me. What is happening in this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone explain to me. What is happening in this code?

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); } } } }

22nd Apr 2021, 6:21 AM
Atul
Atul - avatar
16 Answers
+ 1
"What is the difference between != and =! in Java? - Stack Overflow" https://stackoverflow.com/questions/8825840/what-is-the-difference-between-and-in-java //Go through this also
22nd Apr 2021, 7:28 AM
Atul [Inactive]
+ 2
Sorry Atul I think so I provided you the reverse one 0=false and 1=true. Sorry everyone for it. I was utterly confused
22nd Apr 2021, 7:04 AM
Atul [Inactive]
+ 2
Atul helped Atul and won!
22nd Apr 2021, 7:40 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
why 3? where did you see this in the program? have you checked the launch? the program outputs all odd numbers.
22nd Apr 2021, 6:48 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
i'm blind
22nd Apr 2021, 6:50 AM
Finii
Finii - avatar
+ 1
"Why boolean in Java takes only true or false? Why not 1 or 0 also? - Stack Overflow" https://stackoverflow.com/questions/2015071/why-boolean-in-java-takes-only-true-or-false-why-not-1-or-0-also
22nd Apr 2021, 7:05 AM
Atul [Inactive]
22nd Apr 2021, 1:36 PM
Atul
Atul - avatar
0
for(variable; condition; addition) <-- for starts a loop it checks if the variable(i) is <= 3 (i (<) less than or (=) equal to 3) while the variable(i) is less than 3 it adds +1 to the variable i (i++) i dont know if this is the answer you wanted but i'm not good at explaining
22nd Apr 2021, 6:39 AM
Finii
Finii - avatar
0
🤭 It looks like you need to come up with another explanation
22nd Apr 2021, 6:51 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
why is bulin 1 a lie? where can I read about it? and yes, which one of you is the real Atul? 😁
22nd Apr 2021, 6:55 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
I understand the logic as follows: since the if expression is executed only when the condition is true, and the string b =!b at each iteration of the loop changes the truth to untruth and vice versa, then in the end it turns out that i is printed every other time. brilliant!
22nd Apr 2021, 7:32 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Yes Ярослав Вернигора(Yaroslav Vernigora) inshort it skips one iteration, which looks like odd. I get to know about this =! After looking this code 😅
22nd Apr 2021, 7:38 AM
Atul [Inactive]
0
Haloo
23rd Apr 2021, 1:14 PM
عبدالعزيز عصام
عبدالعزيز عصام - avatar
24th Apr 2021, 3:27 AM
Atul
Atul - avatar
0
Ah! Error... use relational operator... instead of assignment in if statement
24th Apr 2021, 5:49 AM
Aditya Raj Singh
Aditya Raj Singh - avatar