• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

please answer this question.

 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
W Pearce
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
should print true

edit: should probably post things like this in the beginner forums, just a thought
 
Naveen Megharaj
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
x > 3 = false
y < 2 = false
doStuff() = true

so you probably already know

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.
 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats it............!!! thanks a lot to all the repliers.......it was very helpful.......please continue the good work.
once again thanks.....
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 684
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic