return model == DHT11 ? 0 : -40;
I believe it means "return model if between 0 and -40", but I'd like a definitive answer.
return model == DHT11 ? 0 : -40;
I believe it means "return model if between 0 and -40", but I'd like a definitive answer.
Its the ternary operator, equivalent to:
if (model == DHT11)
return 0;
else
return -40;
so it has nothing to do with a check for a range.
The ternary operator yields a value, i.e. you could also use this in an assignment like:
retval = model == DHT11 ? 0 : 40;
return retval;
This operator (E1?E2:E3) is called ternary operator, where E are expressions.
It means : "If E1 is true, return E2, else, return E3"
Here's a link to cppreference explaning a bit more.
As already mentioned, it is called the ternary operator.
The ternary operator checks to see if something is true, and if it is, yields the value before the :
. If it is false, it yields the value after the :
.
In this situation, it checks to see if model == DHT11
evaluates to true, and gives 0 if so. If model == DHT11
evaluates to false, it gives -40. The value the ternary evaluates to is then given to return
, quitting the function, and returning either 0 or -40.
Other example:
std::string hi = "hi";
std::cout << (hi == "hi") ? "string hi contains \"hi\"." : "string hi does not contain \"hi\"." << "\n";
Would print:
string hi contains "hi".
General Syntax:
bool ? value1 : value2
The ternary operator is just like an if-else statement, compacted down to one line.