+ 21
Friends check out my code and explain why it's print odd numbers?
https://code.sololearn.com/cDeE2563TWhb/#java i searched for good answer but can't find any.
7 Réponses
+ 16
boolean b = false;
if (b =! b)
→ 0,2,4,6,8
boolean b = true;
if ( b=! b)
→ 1,3,5,7,9
boolean b = true;
if ( b = b )
→ 0,1,2,3,4,5,6,7,8,9
+ 17
@Frederick when b = !b it puts the value which is not b every time the loop runs.
And that's !b. I wasn't trying !=.
+ 14
@Patrik I know some of it but still wanna get the idea. I don't have lot of time to study about these topics and would search on google but thought why not just ask here.
Thanks @noob, jasmin, Sakib.
+ 4
You ask why but you know the answer ;-)
+ 3
well, the first thing is that =! should be written !=. the next thing is the use of the = which usually means
is assigned, except in visual basic
and use of the == which usually means
equals, except in visual basic
therefore, the lines that look like
boolean =! boolean
evaluate to true
while the line that looks like
boolean = boolean
means boolean is assigned to boolean
which is false by definition,
so the last line that evaluates to true
prints the line of odd integers.
I am trying to figure out how you got this code to compile at all
+ 1
Try to add a newline in print(), that will help ;-)
+ 1
initially...b has value 1... in 1st iteration, if condition changes d value to 0 ... condition becomes false and the argument is skipped and 0 is not printed... in next iteration reverse scenario occurs and condition becomes true and 1 is printed... this is repeated through the loop... hope dis answers the question