+ 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???
14 odpowiedzi
+ 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) 👋
+ 22
Gaurav Agrawal got!! it's awesome!😀...thank a lot for your help!☺
+ 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
😊👋👏👏😊😊
+ 16
thanks a lot to all for contribuating and making this "fact" more understandable for us. Raúl Gracia Maiques thanks again!!
+ 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.
+ 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..😁
+ 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.
+ 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);
}
}
+ 3
if first condition is false, second will not check, and incrementation will not complete. in that situation you can try & operator.
+ 2
hello
+ 1
hello
0
facebook hack
0
oye koi hai
- 5
Mxxx