+ 1

What is the Output and how explain?

public class Program { public static void main(String[] args) { int x=1 ,y=1, z=0; if(!(x++>1 && y++>1)) { z=x+y; } System.out.println(z); } }

24th Apr 2017, 12:30 PM
Rahul M
Rahul M - avatar
2 Answers
+ 7
if(!(x++>1 && y++>1)) Here x++ its Post increment *So first the x is compared to 1 i.e. x>1 is false then x values is incremented So Now x=2. *Since we are using && operator since one condition is false it will not check the other condition since it will be false anyways. So y=1. *!() Not operator makes that condition true . so the inside code gets executed z=x+y; z=2+1; Output : 3
24th Apr 2017, 12:54 PM
Manideep
Manideep - avatar
+ 5
3 x++>1 is false (1=1). x incremented afterwards and equals 2 then (afterwards, in any next lines, evaluations...) Left part of && is false so right part is not evaluated (no need for it) and y is still equals 1. Condition of "if" is true (because !(false) so = true) and z is equal 2 + 1 so it's 3.
24th Apr 2017, 12:56 PM
Andrew Harchenko (Astana-Tomsk)
Andrew Harchenko (Astana-Tomsk) - avatar