+ 1

Why if condition not executes if we only put an integer inside condition in Java.

int x=1; if (x){ Sop("true"); else Sop("false"); Whats wrong here?

30th Aug 2020, 3:35 PM
Devendra Yadav
Devendra Yadav - avatar
9 Answers
+ 3
Devendra Yadav cause x is integer. You should make it boolean.
30th Aug 2020, 4:02 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 2
Charitra Agarwal you are wrong
30th Aug 2020, 4:00 PM
Marina Vasilyova
Marina Vasilyova - avatar
+ 1
This is because, anything which is not null, will give true.. X is 1.. so its true... it will give false only if x is 0... and true in all cases
30th Aug 2020, 3:57 PM
Charitra
Charitra - avatar
+ 1
conditions in java have to be boolean type, not int it is simpler, better readable, unambiguousand and less confusing + there is unclosed bracket { Sop() is not defined here int x=1; if (x==1) System.out.println("true"); else System.out.println("false");
30th Aug 2020, 4:05 PM
zemiak
+ 1
Maria Vasilyova got it thanks
30th Aug 2020, 4:07 PM
Devendra Yadav
Devendra Yadav - avatar
+ 1
zemiak thanks for answering.
30th Aug 2020, 4:10 PM
Devendra Yadav
Devendra Yadav - avatar
+ 1
Maria Vasilyova He took data type as int... I gave answer keeping int in mind... You can experiment it with your compiler once..đŸ€—
30th Aug 2020, 4:18 PM
Charitra
Charitra - avatar
+ 1
Charitra Agarwal https://code.sololearn.com/cFQ3it1sSQQk/?ref=app Good luck You won't run this code on any jvm
30th Aug 2020, 4:23 PM
Marina Vasilyova
Marina Vasilyova - avatar
0
Maria Vasilyova Oh.. I see... Java has a different structure.. This works in c++ and python.
30th Aug 2020, 5:17 PM
Charitra
Charitra - avatar