• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

ternary operator

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Kathy's book has this exercise:



the output is "everthing" but from my understandig it would be "universe"


x = (boolean expression) ? value to assign if true : value to assign if false



On that question it evaluates the first to false which route the code flow to another condition (i>50)?"universe":"everything"; which is true, so the output would be "universe", could anyone please explain this?

Thanks in advance
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Friend,
i = 42;
42 is not greater than 50.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i<40, then the ternary returns "life." Otherwise, a second (nested) ternary is evaluated. In the second ternary, if i>50, then it returns "universe." Otherwise, it returns "everything."

In this example, i is 42. So i<40 evaluates to false, and so the second ternary evaluated. In the second ternary, i>50 also evaluates to false, and so "everything" is returned.

This is a shorthand way of writing...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's on page 301 of the book and it should read:

String s = (i<40)?"life"i>50)?"universe":"everything";

(The is unfortunately interpreted as a sad smiley in your post - note that there's an option to disable smileys when you write a post).

Originally posted by marcelo ribeiro:
On that question it evaluates the first to false which route the code flow to another condition (i>50)?"universe":"everything"; which is true, so the output would be "universe", could anyone please explain this?


Your logic about the flow is correct, but i is 42, so i > 50 is false, not true - so the anwer is "everything".
[ August 27, 2007: Message edited by: Jesper Young ]
 
Marcelo Ribeiro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohh....you're right:


i = 42;
42 is not greater than 50.



I'm becoming crazy studying for this....thank you!!!
 
Barry's not gonna like this. Barry's not gonna like this one bit. What is Barry's deal with tiny ads?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic