return int_length(len > 0 ? len : 1)
what is the meaning of the syntax in the brackets, I keep getting confused when reading this code. thanks
return int_length(len > 0 ? len : 1)
what is the meaning of the syntax in the brackets, I keep getting confused when reading this code. thanks
It is a ternary operator. If len>0
is true result of the expression is len
else its 1
.
if(len > 0)
it will return int_length(len);
else it will return int_length(1);
That's the ternary operator.
It's equivalent to
if (len>0)
return int_length(len);
else
return int_length(1);
it means
if(len > 0)
{
return int_length(len);
}
else
{
return int_length(1);
}
That is the ternary conditional operator. Its an "inline if".
It basically is this
int temp;
if (len > 0)
{
temp = len;
}
else
{
temp = 1;
}
int_length(temp);