• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

please answer this question.

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic