Chellenges | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

16th May 2017, 1:34 AM
Shelby Blehm Dunn
Shelby Blehm Dunn - avatar
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.
16th May 2017, 4:51 PM
Álvaro
+ 1
Awesome! Thank you!
16th May 2017, 6:04 PM
Shelby Blehm Dunn
Shelby Blehm Dunn - avatar
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.
16th May 2017, 5:22 AM
Álvaro
0
Yes, they look like that. What does the question mark mean?
16th May 2017, 2:43 PM
Shelby Blehm Dunn
Shelby Blehm Dunn - avatar