Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Evaluation Order.

 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
from this code


the result is true, false, false
the part i don't understand is that the '&&' operator has a higher priority over '||' therfore it'll be evaluated first, but this is not what is happening.
for a strange reason '||' is evaluated first.
any ideas why?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not understand how b and c are fales. They should be true....
 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi:
boolean x = (a = true) || (b = true) && (c = true); this statement is equilvalent to: boolean x = (a = true) || ((b = true)) && (c = true));
In short ciruit operator OR, if the left side is true, the right side is not evaluated. In the previouse statement <a=true> then the right statement will never be evaluated, and b & c will stay false.
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My understanding: operator precedene does not directly dictate order of evaluation - it dictates how operands get associated with operators. Evaluation proceeds from left to right. So as mentioned above
a + b * c
is treated as:
a + (b * c) and evaluation goes from left to right
as opposed to:
(b * c) + a
You can convince yourself of this with:
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are probably right Steve, however operator evaluation order sometimes gets really uncomfortable.
there is a lot of question regarding this matter in 'Dan Chisholm' exams, for instance...

this is evaluated as: a=(int)((1)+(++a + a++))
Steve, what do you think about this:
to evaluate an expression:
1) Use brackets to seperate operators with higher priorities.
2) always start evaluating from left to right.
for example: a = a + b * c;
1) a = a + (b * c)
2) start evaluating from L --> R
 
Steve Lovelace
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is exactly what compilers are supposed to be doing.
HOWEVER, quoting from Kernighan & Ritchie "...writing code that depends on order of evaluation is a bad programming practive in any language."
HOWEVER, I believe Java specifies left-to-right order more rigorously than the C specification does: evaluation order
 
straws are for suckers. tiny ads are for attractive people.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic