• 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

The Conditional Operator ?:

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I am unable to read this statement written using ? operator.
a = b ? c ? d : e ? f : g : h ? i : j ? k : l ;
If any one can tell me whihc is true part of which and which is false part. To me this statement seems look like this.
if b is true then we go the true part and that is that we check c that if c is true. And if it is true then we finish at d by reutring the value of d.
But what if b is false what will be returned.
And now if we find b true, then c false and e false then we eand up at g. Can someone explain me what wil the case theat we will reach at k or l. Or if some one can write an algorithmic form of this expression.
Regards
Muhammad Ali
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
the statement is to be read like follows,
a = (b ? (c ? d : (e ? f : g)) : (h ? i : (j ? k : l)));
See JLS 15.24
Val
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, i submitted too quickly...
a = (b ? (c ? d : (e ? f : g)) : (h ? i : (j ? k : l)));
1. if b is true then we evaluate (c ? d : (e ? f : g)) (the first part) else the second part (h ? i : (j ? k : l))
2. the same for each part:
if b is true and c is true then a=d
if b is true and c is false and e is true then a=f
if b is true and c is false and e is false then a=g
if b is false and h is true then a=i
if b is false and h is false and j is true then a=k
if b is false and h is false and j is false then a=l
Hope it helps,
Val
 
md ali
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou very much.
Muhammad Ali
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic