programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

Ranch Hand
Posts: 39
int y=5;
int x=2;
if((x>3) && (y<2) || doStuff()) {
System.out.println("true");
}
else {
System.out.println("false");
}
}

//can anybody please explain how d above program segment gets executed if "doStuff()" method returns "true".

Ranch Hand
Posts: 32
should print true

edit: should probably post things like this in the beginner forums, just a thought

Naveen Megharaj
Ranch Hand
Posts: 39
i know that it will print true, but i want to know how the "condition" with in the "if statement" simplifies.........

W Pearce
Ranch Hand
Posts: 32
x > 3 = false
y < 2 = false
doStuff() = true

true AND true = true
true AND false = false
true OR true = true
true OR false = true

so basically your if statement breaks down to

as long as you have an OR that evaluates to true it will be evaluated as true

however something likewould evaluate to false. always work from inside parenthesis to outside. simplified this could be represented as and simplified still further would be
Hope that helps at all.

author
Sheriff
Posts: 23384
128

Naveen Megharaj wrote:i know that it will print true, but i want to know how the "condition" with in the "if statement" simplifies.........

A couple of points.... First, the logical AND has higher precedence than the logical OR, so the condition would be like this (if using parens)....

Second, the logical AND is a short circuit operator, so when the "x>3" portion is false, it will skip the check for the "y<2", and immediately return false.

Third, the logical OR is also a short circuit operator, but can't short circuit because the left side is false. It will call the doStuff() method which returns true -- and generates a logical OR result of true.

Hence, the condition in the "if" statement is true...

Henry

Naveen Megharaj
Ranch Hand
Posts: 39
thats it............!!! thanks a lot to all the repliers.......it was very helpful.......please continue the good work.
once again thanks.....

Ranch Hand
Posts: 686
I don't see the actual doStuff() method here, but it must be a Boolean method right? How can we say it returns true if we don't know the method?

Henry Wong
author
Sheriff
Posts: 23384
128

Fred Hamilton wrote:I don't see the actual doStuff() method here, but it must be a Boolean method right? How can we say it returns true if we don't know the method?

This was stated in the original post -- that the doStuff() method returns true.

Henry

Fred Hamilton
Ranch Hand
Posts: 686

Henry Wong wrote:

Fred Hamilton wrote:I don't see the actual doStuff() method here, but it must be a Boolean method right? How can we say it returns true if we don't know the method?

This was stated in the original post -- that the doStuff() method returns true.

Henry

ahh right you are. I had not picked up on that.

Sheriff
Posts: 57832
178
But if the doStuff() method doesn't return a boolean, the compiler will pick up a discrepancy between the || operator and the return type, and refuse to compile your code.