Results 1 to 6 of 6
Thread: How to read this if statement?
- 11-25-2011, 10:20 AM #1
Member
- Join Date
- Nov 2011
- Location
- Schijndel, Netherlands
- Posts
- 12
- Rep Power
- 0
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!
- 11-25-2011, 11:09 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,Java Code:if (onlyNots) return bonnr == 0; else return bonnr != 0;
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-25-2011, 11:30 AM #3
Member
- Join Date
- Nov 2011
- Location
- Schijndel, Netherlands
- Posts
- 12
- Rep Power
- 0
Re: How to read this if statement?
Thank you for the nice explanation, I'm understanding it now!
- 11-26-2011, 05:30 PM #4
Senior Member
- Join Date
- Nov 2010
- Posts
- 210
- Rep Power
- 3
Re: How to read this if statement?
Alternatively, the expression could simply have been replaced with:
Java Code:return onlyNots == (bonnr == 0);
- 11-26-2011, 05:47 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,401
- Blog Entries
- 7
- Rep Power
- 17
- 11-26-2011, 06:05 PM #6
Devil
- Join Date
- Nov 2011
- Location
- Pakistan
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
How to read currency money from Notpade instead of read all information from web
By Albert050 in forum NetBeansReplies: 0Last Post: 11-15-2011, 09:13 PM -
for statement
By mattcash83 in forum New To JavaReplies: 8Last Post: 10-18-2011, 02:48 AM -
Can't get my "if" statement to read user input
By daletron3030 in forum New To JavaReplies: 7Last Post: 01-16-2009, 05:24 AM -
java.io.IOException: Unable to read entire block; 493 bytes read before EOF; expected
By kushagra in forum New To JavaReplies: 5Last Post: 10-17-2008, 02:13 PM -
Statement or Prepared Statement ?
By paty in forum JDBCReplies: 3Last Post: 08-01-2007, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks