If without else ternary operator

Z.V picture Z.V · Nov 18, 2013 · Viewed 63k times · Source

So far from I have been searching through the net, the statement always have if and else condition such as a ? b : c. I would like to know whether the if ternary statement can be used without else. Assuming i have the following code, i wish to close the PreparedStatement if it is not null

(I am using Java programming language.)

PreparedStatement pstmt;

//.... 

(pstmt!=null) ? pstmt.close : <do nothing>;

Answer

frankie liuzzi picture frankie liuzzi · Nov 18, 2013

No, you cannot do that. Instead try this:

if(bool1 && bool2) voidFunc1();