Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By PhHein
  • 1 Post By gimbal2

Thread: Ternary Operator (?:) Questions

  1. #1
    Mind Blown is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Ternary Operator (?:) Questions

    I had quite a hard time figuring this out my self (it came from the book and I am trying to analyze it) but... pff <_< no hope. Been trying to figure it out for an hour and yet I still couldn't get it.

    So here is the question:

    - How to convert this if/else statement using the ternary (?:) operator?
    Java Code:
    double value = 8.9;
    if (value > 10.6)
         System.out.println("x is greater than 10. \n");
    else
         System.out.println("x is less than or equal to 10. \n");

    If you do know it, please tell me what you did and how did you done it (line by line) if you may so that I can understand it properly.

    Thanks in advance.
    Last edited by Mind Blown; 12-11-2013 at 04:53 PM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Ternary Operator (?:) Questions

    As it is only one line it's hard to tell line by line:
    Java Code:
     System.out.println(value > 10.6?"x is greater than 10. \n":"x is less than or equal to 10. \n");
    Mind Blown likes this.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Ternary Operator (?:) Questions

    I think the intended result is that those four lines of the if/else statements turn into 1 line that does a single System.out, so there is no "line by line" to speak of.

    Okay lets say I give you a simple example and not solve the riddle for you directly.

    Java Code:
    boolean truthOrDare = true;
    
    String text = truthOrDare ? "truth" : "dare";
    Can you then apply this to your problem?

    EDIT: ninja'd by the spoonfeeding PhHein! Tsk tsk.
    Mind Blown likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Mind Blown is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Ternary Operator (?:) Questions

    Quote Originally Posted by PhHein View Post
    As it is only one line it's hard to tell line by line:
    Java Code:
     System.out.println(value > 10.6?"x is greater than 10. \n":"x is less than or equal to 10. \n");
    Heh, my bad I've no idea actually.

    Now I finally understand it.
    Thanks man.

  5. #5
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Ternary Operator (?:) Questions

    Quote Originally Posted by gimbal2 View Post
    EDIT: ninja'd by the spoonfeeding PhHein! Tsk tsk.
    Yeah, sorry, mate. I couldn't think of a non spoonfeeding way. I'm not as creative as you :)

    Phil
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Ternary Operator (?:) Questions

    Not sure if serious or poking fun at me in return... I hope the latter.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Ternary Operator (?:) Questions

    Quote Originally Posted by gimbal2 View Post
    Not sure if serious or poking fun at me in return... I hope the latter.
    You could do this:

    Java Code:
    System.out.println("x is "+(value > 10.6?"great than":"less than or equal to")+" "+(42-32));
    kindest regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default Re: Ternary Operator (?:) Questions

    Quote Originally Posted by gimbal2 View Post
    Not sure if serious or poking fun at me in return... I hope the latter.
    The latter, of course.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default Re: Ternary Operator (?:) Questions

    Quote Originally Posted by PhHein View Post
    The latter, of course.
    <high squeaky voice>
    You're lying old woman! Now confess! It was the first! Confess!
    </high squeaky voice>

    kindest regards,

    The Spanish Inquisition
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Help on Java Coding Decimal and Ternary Operator
    By TheGreenG in forum New To Java
    Replies: 1
    Last Post: 06-13-2013, 02:42 PM
  2. Replies: 5
    Last Post: 01-22-2012, 12:02 AM
  3. Shorthand ternary operator ?
    By Billywizz in forum New To Java
    Replies: 3
    Last Post: 10-02-2011, 10:57 PM
  4. doubt regarding ternary operator (?)
    By subith86 in forum New To Java
    Replies: 2
    Last Post: 03-10-2011, 09:15 AM
  5. Replies: 2
    Last Post: 02-27-2010, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •