Trying to understand why a '?' and ':' (colon) are used in a BME280 calculation.

int32_t var1
 
 var1 = (var1 < 0 ? 0 : var1)
 var1 = (var1 > 419430400 ? 419430400 : var1)

Can someone help me understand this code from a BME280 humidity
sensor. I am interested in how the ? (question mark) and : (colon)
is used, and how it changes or modifies var1.

Thanks

Bill M.

Search engines doesn't like the ?, :

Try a search for "Arduino ternary".

Equivalent to

if (var1 > 419430400)
{
  var1 = 419430400;
}
else
{
  var1 = var1;
}

but could be reduced to

if (var1 > 419430400)
{
  var1 = 419430400;
}

Thank you for your prompt replies

link to the documentation of the conditional operator (colloquially referred to as ternary operator or condition) for whoever would end up reading this post in the future

// checks the boolean value of the first expression (a) and, 
// depending on the resulting value, evaluates and returns 
// either the second (b) or the third (c) expression
a ? b : c;

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.