+ 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.

27th Feb 2017, 12:48 PM
Ram chandra Giri
Ram chandra Giri - avatar
7 Answers
+ 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
27th Feb 2017, 1:29 PM
Jasmin
Jasmin - avatar
+ 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 !=.
5th Mar 2017, 11:51 AM
Ram chandra Giri
Ram chandra Giri - avatar
+ 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.
27th Feb 2017, 1:44 PM
Ram chandra Giri
Ram chandra Giri - avatar
+ 4
You ask why but you know the answer ;-)
27th Feb 2017, 1:10 PM
Patrik Sokol
Patrik Sokol - avatar
+ 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
5th Mar 2017, 11:47 AM
Frederick Eccher
Frederick Eccher - avatar
+ 1
Try to add a newline in print(), that will help ;-)
27th Feb 2017, 1:22 PM
n_OO_b
n_OO_b - avatar
+ 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
27th Feb 2017, 1:40 PM
Sakib SKS
Sakib SKS - avatar