0
Chellenges
I keep trying the challenges and I'm seeing question marks within the code examples....but I haven't seen anything that explains what the ? does within the code. What does the ? do in code?
4 Answers
0
OK, that's called a ternary operator âit takes 3 arguments.
In general, if you have
a ? b : c;
that's equivalent to
if (a) { b } else { c };
That is, "a" is evaluated as either true or false. If it's true, then "b" is executed. If it's false, then "c" is executed.
For example,
answer = ( x>0 ? "positive" : "negative or zero" );
will assign the string "positive" to the variable "answer" if x>0, and the string "negative or zero" otherwise.
+ 1
Awesome! Thank you!
0
Does it look like this?
int x = ( check ? 25 : 0 );
That is, a ? preceded by a boolean expression or variable, followed by an expression, a colon : and another expression.
0
Yes, they look like that. What does the question mark mean?