Single return ternary operator

I wonder if it is possible to write a three-dimensional operator for a single return. I tried Google on the Internet but could not find the answer. or is it not called a ternary operator?

Thanks so much for your advice.

If(A == 1) execute_function(); into A == 1 ? execute_function() //???Possible???

+4
source share
4 answers

Yes

 (exists == 1) ? execute_function() : false; 

runs the function if true else wont exists

Added: It is best to do the following:

 if( A == 1 ) { execute_function(); } 

As the use of the ternary operator in the above case is not so fruitful, since you check only the true side of the condition and do not care about what is on the false side.

+5
source

Here is the shortest way.

 A == 1 && execute_function(); 
+11
source
+2
source

As already mentioned, A == 1 && execute_function(); The shortest. However, another option is to use a single if line:

if( A == 1 ) execute_function();

0
source

All Articles