+ 26

[SOLVED] ?????👋 Please a question... about this code.

int a=2, b=3; if (a++ > b && ++b > 0) //a =2 , b = 4 I'm ok!! System.out.println(1); else System.out.print(a+""+b); // output : 33 //a =3 I'm ok! then why b=3 but no more 4 ???? I thought that the fact that the condition (a++ > b) is false in the left side of the operator && ( I mean 2 > 3 ) has not affected the content of a variable (but keep his value for the next incrementation). in other hand,the fact that ++b > 0 in the right side of the operator && ( I mean 4 > 0 ) is true ,so this result belongs to testing the condition of the content of b variable under the number 0 but not about the incrementation of b variable????😕(is it just a boolean??) I'm right or there is another problem with my logic???

14th Jun 2018, 6:11 PM
Hans Larry Mba Bekale
Hans Larry Mba Bekale - avatar
14 Respostas
+ 35
a++>b 2++>3 //thats false since , we are taking && as logical operator ... so by 1st value as false ... we are sure that overall result will be false ... no need to check whether b++>0 [ie, that never got executed ... ie b++>0 ... that expression is never reached ... so no increment in value of b] //so a=3 & b remains 3 //hope it helps & bye bye(final) 👋
14th Jun 2018, 6:18 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 22
Gaurav Agrawal got!! it's awesome!😀...thank a lot for your help!â˜ș
14th Jun 2018, 6:29 PM
Hans Larry Mba Bekale
Hans Larry Mba Bekale - avatar
+ 17
thats why the output of this code int x=0,y=0; if(++x>0 && ++y> 0) //both side of && are true System.out.print(x+""+y); //output 11 😊👋👏👏😊😊
14th Jun 2018, 6:51 PM
Hans Larry Mba Bekale
Hans Larry Mba Bekale - avatar
+ 16
thanks a lot to all for contribuating and making this "fact" more understandable for us. RaĂșl Gracia Maiques thanks again!!
15th Jun 2018, 8:09 AM
Hans Larry Mba Bekale
Hans Larry Mba Bekale - avatar
+ 5
see the answer to your question is that the if statement always searches for true value and so it moves and executes the other condition only if the first condition is true. if we think according to the computer then we as computer will first check the if statement and as first condition if only false we will not go to the second condition and come out of the block. remember that the && in if always sees if both values are true and if it's first value is false then it will never go to the next value. i think this answer should satisfy you.
15th Jun 2018, 4:29 AM
Shubh Mehrotra
Shubh Mehrotra - avatar
+ 4
Don't know about Java, but in Python, if we use "and" operator and first condition is false, Even errors will be ignored in second condition..😁
15th Jun 2018, 5:13 AM
Zoetic_Zeel
Zoetic_Zeel - avatar
+ 4
when you use doble operator && in a condition statement, first of all check left part and only if its true continue. with the operator || It happens the opposite. if (true || false) the second part is not reached, because its not necessary. try the same Code with: int a=3,b=3; if (++a > b && ++b > 0) thank you for the question, this kind of questions appears on OCA exams.
15th Jun 2018, 8:05 AM
RaĂșl Gracia Maiques
RaĂșl Gracia Maiques - avatar
+ 4
public class Program { public static void main(String[] args) { int a=2,b=3; if (a<b&&b>0) System.out.println("1"); else System.out.println(b); } }
17th Jun 2018, 4:41 PM
Christian lLONGA BOMANKOY
Christian lLONGA BOMANKOY - avatar
+ 3
if first condition is false, second will not check, and incrementation will not complete. in that situation you can try & operator.
15th Jun 2018, 3:44 AM
Chered
+ 2
hello
16th Jun 2018, 5:27 PM
Sukhwinder Singh
Sukhwinder Singh - avatar
+ 1
hello
17th Jun 2018, 4:23 PM
Mando Bati
Mando Bati - avatar
0
facebook hack
16th Jun 2018, 5:28 PM
Sukhwinder Singh
Sukhwinder Singh - avatar
0
oye koi hai
16th Jun 2018, 5:46 PM
NITESH RAO
NITESH RAO - avatar
- 5
Mxxx
15th Jun 2018, 6:50 PM
Channa Channa
Channa Channa - avatar