This is the C ternary operator (Objective-C is a superset of C):
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
is semantically equivalent to
if(inPseudoEditMode) {
label.frame = kLabelIndentedRect;
} else {
label.frame = kLabelRect;
}
The ternary with no first element (e.g. variable ?: anotherVariable) means the same as (valOrVar != 0) ? valOrVar : anotherValOrVar
This is the C ternary operator (Objective-C is a superset of C):
label.frame = (inPseudoEditMode) ? kLabelIndentedRect : kLabelRect;
is semantically equivalent to
if(inPseudoEditMode) {
label.frame = kLabelIndentedRect;
} else {
label.frame = kLabelRect;
}
The ternary with no first element (e.g. variable ?: anotherVariable) means the same as (valOrVar != 0) ? valOrVar : anotherValOrVar
It's the ternary or conditional operator. It's basic form is:
condition ? valueIfTrue : valueIfFalse
Where the values will only be evaluated if they are chosen.
It kind of seems like an abstract if statement, where
?checks for truth and:is else... is that so?
Yeah, almost. It's called the "conditional operator" (sometimes not entirely accurately referred to as "the ternary operator", since it's the only ternary operator in C). It's not a statement though, it's an expression, it has a value. It evaluates to its second argument if the first argument evaluates to true, and to its third argument if it's false. Therefore
sign = (s[i] == '-') ? -1 : 1;
is equivalent to
if (s[i] == '-') {
sign = -1;
} else {
sign = 1;
}
It kind of seems like an abstract if statement.
That's correct. This is called a "ternary conditional operator".
The normal if works on statements, while the conditional operator works on expressions.
I am wondering if there is a particular reason it seems to often be replaced by a formal if/else--besides, perhaps, readability?
There are cases where branching on statements is not enough, and you need to work on the expression level.
For instance, consider initialization:
const int foo = bar ? 5 : 3;
This could not be written using a normal if/else.
Anyway, people who are saying it's equivalent to the if/else are being imprecise. While the generated assembly is usually the same, they are not equivalent and it should not be seen as a shorthand version of if. Simply put, use if whenever possible, and only use the conditional operator when you need to branch on expressions.
A traditional if-else construct in C, Java and JavaScript is written:
if (a > b) { result = x; } else { result = y; } This can be rewritten as the following statement:
result = a > b ? x : y;
Can the ? operator be used this way in Elixir? I don't find anything and all the conditional statements include do-end which looks so bothersome and it's hard to believe Elixir might not support such a quick and clean way.
Hi I'm using codeblocks with the gcc++ compiler, and I want to use a global variable Boolean as the condition for a question mark operator, here is the code I'm having a n issue with.
bool moving = false;
moving ? switch(direction){
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
: ;but the compiler doesn't seem to recognize "moving" as a condition, because whenever I try to compile it, it tells me "error - expected primary condition before switch"
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.