Unlike all other operators in Objective-C—which are either unary or binary operators—the conditional operator is a ternary operator; that is, it takes three operands. The two symbols used to denote this operator are the question mark ( ? ) and the colon ( : ).
Scope resolution operator :: (C++ only) The :: (scope resolution) operator is used to qualify hidden names so that you can still use them. You can use the unary scope operator if a namespace scope or global scope name is hidden by an explicit declaration of the same name in a block or class.
It's the null conditional operator. It basically means: "Evaluate the first operand; if that's null, stop, with a result of null.
The conditional operator is a single programming statement and can only perform one operation. The if-else statement is a block statement, you can group multiple statements using a parenthesis. The conditional operator can return a value and so can be used for performing assignment operations.
This is commonly referred to as the conditional operator, and when used like this:
condition ? result_if_true : result_if_false
... if the condition
evaluates to true
, the expression evaluates to result_if_true
, otherwise it evaluates to result_if_false
.
It is syntactic sugar, and in this case, it can be replaced with
int qempty()
{
if(f == r)
{
return 1;
}
else
{
return 0;
}
}
Note: Some people refer to ?:
it as "the ternary operator", because it is the only ternary operator (i.e. operator that takes three arguments) in the language they are using.
This is a ternary operator, it's basically an inline if statement
x ? y : z
works like
if(x) y else z
except, instead of statements you have expressions; so you can use it in the middle of a more complex statement.
It's useful for writing succinct code, but can be overused to create hard to maintain code.
Just a note, if you ever see this:
a = x ? : y;
It's a GNU extension to the standard (see https://gcc.gnu.org/onlinedocs/gcc/Conditionals.html#Conditionals).
It is the same as
a = x ? x : y;
You can just rewrite it as:
int qempty(){ return(f==r);}
Which does the same thing as said in the other answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With