Haha, just to clarify the clarifications, the ? : operator is called the ternary operator. It is in the form of
boolean ?
consequence :
alternative
If boolean is true, evaluate consequence, else evaluate alternative. Note that this is NOT returning anything, it runs it just like code. You can do, for example
bool_variable ? function_if_true() : function_if_false();
It's basically a shorthand of
if(bool_variable){
function_if_true();
}else{
function_if_false();
}
However, since that fits in one line, and it will be evaluated in place, not as a seperate block, the ternary operator is more than a shorthand of if-else. For example, consider the following:
result = bool_variable ? function_if_true() : function_if_false();
Notice how you cannot do that with an if-else:
result =
if(bool_variable){
function_if_true();
}else{
function_if_false();
}
will not work