• 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

Expression evaluation on an if statement

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please advice on the best way to determine which order an expression will be evaluated.

For example, in the following two statements





The first one is easy enough but how do i determine whether the second one is saying

1. if both x>3 and y < 2 are true or just 1==3 is true

or

2. if x>3 is true and y<2 or 1==3

Thanks
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I guess the first thing youd have to do is learn the general orders of the operators. I think this link below will suffice for that:

http://download.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Once you learn the general workings of that you should understand how you can add your own parenthesis to make sense of things.

In this case since the | has higher precedence than the && the evaluation of the bottom could be changed to this:



I added parenthesis around the == to show the precedence of that as well since == has higher precedence than |.

This is my understandinf of it anyway...if I'm wrong please correct me!
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the K & B book on page 333 it says:



which now prints...nothing! Because the preceding code (with one less set of
parentheses) evaluates as though you were saying, "If (x > 3) is true, and either
(y < 2) or the result of doStuff() is true, then print true." So if (x > 3) is not
true, no point in looking at the rest of the expression." Because of the short-circuit
&&, the expression is evaluated as though there were parentheses around (y < 2) |
doStuff(). In other words, it is evaluated as a single expression before the && and a
single expression after the &&.


They don't talk about | having a higher precedence than &&. I don't really understand what is meant by the above. It seems to me like 'It just stops after the first expression (X>3) as x>3 is not true and there is a && coming next, ignoring anything that comes further right.' Can anybody explain??

Thanks,

John
 
Saloon Keeper
Posts: 15608
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bitwise (including non-shorting) operators have a higher precedence than logical operators. So (y<2) | doStuff() is performed as if it had parentheses around it; because | has a higher precedence than &&.

Since the left hand of && evaluates to false, there is no point in evaluating its right hand side; which in this case is the entire rest of the statement (because of the "hidden" parentheses).
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Precedence is covered in all aspects mathematical logic operations. Maybe you are familiar with PEMDAS in that case. As far as java goes, I put the link in my post regarding the java precedences. Obviously, java (edit: Actually the parser of the compiler would handle this I suppose) handles these for you within your code, BUT if you dont know what the precedences are you may get undesired results. I dont now if k&B explicitly says "precedences", I mean im reading through the book myself currently, and I dont remember lol.

Anyways, when you are grouping your expressions you place the higher precedence stuff together (implicitly | explicitly). Its just like when you look at 1 + 2 * 3. Without parenthesis or precedences you wouldnt know whether to evaluate left to right or actually group the multiplication. Implicitly, most know that we want to do multiplication first. Explicitly, you could look at it as 1 + (2*3) with multiplication having higher precendence than addition.

Regarding what you are looking at...

They showed the first example as:


This shows an extra pair of parenthesis around &&. Since parenthesis have higher precedence than the operators you evaluate x>3 && y <2 together -> false, but they said doStuff() -> true hence it prints out true because the |.

Now in your case it wont print out true because the short circuit operator and x>3 -> false. So you are correct in that assumption. So with just one more set of parenthesis missing you get different results of the same expressions.

What you said in your post is correct, but what they are trying to point out is that sometimes those expressions can look ambigious, and knowing the precedences matters in some cases.
 
Stephan van Hulst
Saloon Keeper
Posts: 15608
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scotty Mitchell wrote:[...] you wouldnt know whether to evaluate left to right [...]



Sorry for nitpicking, but expressions are always evaluated from left to right.
 
Scotty Mitchell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok thats fair to say. To be more clear, I guess I should of said you wouldnt know the order to evaluate left to right because you might need grouping if the expression does not clearly show the precedences.
 
John Stark
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, makes sense now.

John
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic