• 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

about ?: operator

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

Output: hello
Explanation:
The operands are grouped from the right to the left, thus, the expression can be rewritten as (b=!b)?((b=!b)?"Hello":"hello") : ( (b=!b)?"world":"World"). b is initially false. When evaluating the first b=!b, b will be assigned true and the second operand (that is (b=!b)?"Hello":"hello") will be evaluated. b is now true and is assigned its opposite value (false). The second operand ("hello") is thus evaluated.
The association of "?:" is right-to-left, if we explained the above code as the following steps, is it right?
1. s = (b=!b)?(b=!b)?"Hello":"hello" : ( (b=!b)?"world":"World"); //b is true now
2. s = (b=!b)?((b=!b)?"Hello":"hello"):"world"
3. s = (b=!b)?"hello":"world" //b is false now
4. s = "hello"
[Edited by Val to reformat the code]
[ October 06, 2002: Message edited by: Valentin Crettaz ]
 
Claire Yang
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, the frown face should be a semicolon ";" followed by a question mark "?", I don't know why the 2 marks appear as the frown face.
[ October 06, 2002: Message edited by: Claire Yang ]
 
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
Syntactically right-associative means that prior to evaluating the expression, the latter is grouped so as to be able to univoquely evaluate the expression. No evaluations of the conditions are made yet, only after grouping the expression together.
Again a?b:c?d:e?f:g is first "grouped" as a ? b : (c ? d : (e ? f : g)) but the boolean expression a, c and e have not yet been evaluated.
Please refer to JLS 15.25 Conditional Operator ? : for detailed information about the ?: ternary operator
Sorry, the frown face should be a semicolon ";" followed by a question mark "?", I don't know why the 2 marks appear as the frown face.
The "instant Graemlins" got you
[ October 06, 2002: Message edited by: Valentin Crettaz ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic