Results 1 to 8 of 8
  1. #1
    mwilliams is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Tenary condition operator

    Question about tenary condition operator. Example is below which used a tenary condition. Not sure why someone would someone use it. Is this used in Java? Also wondering if someone could show me how this would normally be done?


    Java Code:
    circle(g, (nState == 5) ? Color.red : Color.red.darker(), 150, 100);
    circle(g, (nState == 7) ? Color.orange : Color.orange.darker(), 150, 150);

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Have a look here : Java Ternary Operator

    You can use Google as well and find out many more resources related to the same.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Im not sure what you mean, a ternary operator is basically a short hand for if else clauses.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Quote Originally Posted by ranjiths View Post
    A ternary operator is a shorter statement used instead of the if then else statements

    If then else structure:

    if(condition)
    stemnet1;
    else
    statement2;

    The above can be replaced by
    condition?statemnet1:statemnet2;
    No, it can't.

    This is just one of many poor responses you have posted on these forums. Are you just trying to promote your precious blog?

    Abuse reported.

    db

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,236
    Rep Power
    19

    Default

    Ternary operator can be used in this way.
    Not in the way you showed at #4, it can't. The second and third arguments to the ternary are value expressions not statements.

    db

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

    Default

    Quote Originally Posted by Darryl.Burke View Post
    Not in the way you showed at #4, it can't. The second and third arguments to the ternary are value expressions not statements.

    db
    That's not what the other poster said; it is supposed to work with stemnets and statemnets. Personally I've never seen a stemnet (nor a statemnet) so I can't judge here ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by JosAH View Post
    That's not what the other poster said; it is supposed to work with stemnets and statemnets. Personally I've never seen a stemnet (nor a statemnet) so I can't judge here ;-)
    This is by far the funniest (out of so many) comments I have seen from you Jos.

    I almost fell down from my chair after reading this. :D It was just too much for me to handle!

    By the way, this was my previous favorite ;)

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

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

    Default

    Quote Originally Posted by ranjiths View Post
    What do you get by pointing out typographical errors in that way?
    The giggles, why ask?

    knid rards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Waiting on a condition
    By nephos in forum New To Java
    Replies: 9
    Last Post: 04-22-2011, 10:05 AM
  2. string and condition problem
    By durdanto in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 08:20 AM
  3. basic condition
    By ts96 in forum New To Java
    Replies: 1
    Last Post: 02-16-2011, 12:23 PM
  4. While loop condition
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 10-10-2010, 01:14 AM
  5. IF-Condition in a String
    By lenaz in forum Advanced Java
    Replies: 1
    Last Post: 07-18-2009, 12:07 PM

Posting Permissions

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