[英]How does this code work and what is it called
The code in question is "? something : something_else". 有问题的代码是“?something:something_else”。 Usually in the code below you can put either I2C_SLAVE or I2C_SLAVE_FORCE.
通常,在下面的代码中,您可以放置I2C_SLAVE或I2C_SLAVE_FORCE。 But this code does something else.
但是这段代码还有其他作用。 How does it work and what exactly does it do?
它是如何工作的以及它到底是做什么的?
if(ioctl(state.i2c_bus_address, force ? I2C_SLAVE_FORCE : I2C_SLAVE, add) < 0)
{
logger.fail("i2c select fail %d",add);
return -1;
}
It's called the ternary conditional operator. 它称为三元条件运算符。 It's like an if, but inline.
这就像一个if,但是内联。 Here's the format
这是格式
boolean ? result evaluated to if true : result evaluated to if false
Here's an example: 这是一个例子:
y = x>2 ? 12 : 5;
If x is greater than 2, y will be 12, otherwise y will be 5. 如果x大于2,则y将为12,否则y将为5。
It's name is "conditional operator". 它的名称是“条件运算符”。
condition ? expression1 : expression2
If condition evaluates to true, then evaluate expression1 , otherwise evaluate expression2 . 如果condition评估为true,则评估expression1 ,否则评估expression2 。
Not sure if this is what you're after, but the statement ? if_true : if_false
不知道这是您要追求的,但是
statement ? if_true : if_false
statement ? if_true : if_false
control flow is called the ternary operator. statement ? if_true : if_false
控制流称为三元运算符。
The statement
is evaluated. 该
statement
被评估。 If it's true, the expression after the :
is evaluated. 如果为true,则对
:
之后的表达式求值。 Otherwise, the expression after the :
is evaluated. 否则,将评估
:
之后的表达式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.