Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2011
    Location
    Schijndel, Netherlands
    Posts
    12
    Rep Power
    0

    Smile How to read this if statement?

    I have encountered an if-statement in a way I'm unfamiliar with.
    After some googling I can't find a answer that's why I'm asking it here.

    boolean onlyNots;
    Long bonnr;

    return ( onlyNots ? bonnr==0 : bonnr != 0 );

    Can anyone explain to me what the '?' and the ':' means? And how I'm supposed to read this statement?
    When does it return true, when does it return false?

    Many thanks in advance!

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

    Default Re: How to read this if statement?

    It's a ternary expression; its general form is <condition>?<if_true>:<if_false>; if <condition> is true then the value of the entire expression is <if_true>, otherwise the value is <if_false>. In your example the value of the entire expression depends on onlyNots; if it is true the value of the entire expression is bonnr==0 (that is either the value true or false); if the value of onlyNots is false the value of the entire expression is bonnr != 0 (which is also true or false. The return statement could've been written as:

    Java Code:
    if (onlyNots)
       return bonnr == 0;
    else
       return bonnr != 0;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Nov 2011
    Location
    Schijndel, Netherlands
    Posts
    12
    Rep Power
    0

    Default Re: How to read this if statement?

    Thank you for the nice explanation, I'm understanding it now!

  4. #4
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: How to read this if statement?

    Alternatively, the expression could simply have been replaced with:

    Java Code:
    return onlyNots == (bonnr == 0);

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

    Default Re: How to read this if statement?

    Quote Originally Posted by Iron Lion View Post
    Alternatively, the expression could simply have been replaced with:

    Java Code:
    return onlyNots == (bonnr == 0);
    That is 'simply' after a Karnaugh map has been scrutinized and solved ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Mr777 is offline Devil
    Join Date
    Nov 2011
    Location
    Pakistan
    Posts
    12
    Rep Power
    0

    Default Re: How to read this if statement?

    It's also called the Conditional expression, and it behaves as simple if-else. As well defined by JosaH, it has three parts so it's called ternary condition or ternary operator.
    It's general syntax is
    Java Code:
    (condition)?TruePart:FalsePart;

Similar Threads

  1. Replies: 0
    Last Post: 11-15-2011, 10:13 PM
  2. for statement
    By mattcash83 in forum New To Java
    Replies: 8
    Last Post: 10-18-2011, 03:48 AM
  3. Can't get my "if" statement to read user input
    By daletron3030 in forum New To Java
    Replies: 7
    Last Post: 01-16-2009, 06:24 AM
  4. Replies: 5
    Last Post: 10-17-2008, 03:13 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 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
  •