Win a copy of Rust Web Development this week in the Other Languages 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

conditional expression chaining

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
any one pls explain the chain , how it is evaluated
thanks,
Kishore



(disabled smilies)
[ January 10, 2007: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai,

can you please keep the code in a proper format...Am not able to get the second line...

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

The conditional operator is syntactically right-associative (it groups right-toleft)



Therefore,
boolean b1 = false?false:true?false:true?false:true;

Evaluates as
boolean b1 = false ? false : ( true ? false : ( true ? false : ( true ) ) );
[ January 10, 2007: Message edited by: Aniket Patil ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take the first case
boolean b1 = false?false:true?false:true?false:true;
it has got 3 parts
1 - that which is coming before ? --> false
2 - one that is between ? and : --> false
3 - which is placed after : --> true?false:true?false:true;


since the boolean expression (one that is coming in the first part)is having false value, the statement coming after :
is evaluated.ietrue?false:true?false:true;
Again that is another conditional statement. so split that into 3 parts
1 --> true
2 --> false
3 --> true?false:true;

since here the boolean expression is true, it will have the value placed in the second part ie false


take the third case

boolean b3 = ((false?false:true)?false:true)?false:true;

SECTION 1 --> ((false?false:true)?false:true) ? false : true;
Because of parantheses, the 3 parts will be
1 --> ((false?false:true)?false:true)
2 --> false
3 --> true;

SECTION 2 --> (false?false:true)?false:true)
Evaluate the first part which is itself another conditional expression. So split that again
11-->(false?false:true)
12--> false
13 --> true

SECTION 3 --> false?false:true
Again split the first part of 11
111--> false
112--> false
113 --> true

since 111 is false the third part will be returned which is having the value true.

This value will be substituted in SECTION 2. So it become
(true)?false:true
From here false value will be returned and substituted in SECTION 1.

so it become false? false : true
thus b3 will have the value true

You can test the second case by yourself.
 
reply
    Bookmark Topic Watch Topic
  • New Topic