Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Short IF ELSE syntax in objective C

Is there any short syntax for if-else statement in objective C like PHP:

if($value) return 1; else return 0; 

shorter version:

return $value?1:0; 
like image 539
Firdous Avatar asked Mar 21 '12 13:03

Firdous


People also ask

How do you write if else in Objective-C?

An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement. An if can have zero or one else's and it must come after any else if's. An if can have zero to many else if's and they must come before the else.

What is #if in Objective-C?

The syntax of an if statement in Objective-C programming language is − if(boolean_expression) { /* statement(s) will execute if the boolean expression is true */ } If the boolean expression evaluates to true, then the block of code inside the if statement will be executed.

What is Objective-C in iOS?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.


2 Answers

Yes.

Example (pseudo):

value = (expression) ? (if true) : (if false); 

Based on your example (valid code):

BOOL result = value ? YES : NO;  
like image 196
Alladinian Avatar answered Sep 19 '22 20:09

Alladinian


It's exactly the same in both languages, except you typically don't find $ signs in Objective-C variable names.

if(value) return 1; else return 0; 
return value?1:0; 

You should also keep in mind that the conditional operator ?: isn't a shorthand for an if-else statement so much as a shorthand for a true vs false expression. See the PHP manual.

like image 43
BoltClock Avatar answered Sep 17 '22 20:09

BoltClock