MD *_* XF 4 c if-statement keyword
我们都知道,C有if陈述,以及家庭的部分是else if和else语句.该else自行检查,如果没有价值观的成功,如果是这样,运行程序代码.
我想知道是否有类似的东西else检查所有值是否成功而不是它们都没有.
假设我有这段代码:
if (someBool)
{
someBit &= 3;
someFunction();
}
else if (someOtherBool)
{
someBit |= 3;
someFunction();
}
else if (anotherBool)
{
someBit ^= 3;
someFunction();
}
else
{
someOtherFunction();
}
Run Code Online (Sandbox Code Playgroud)
当然,我可以缩短它:
goto(哎呀,不知道为什么我不这样做)if (someBool || someOtherBool || anotherBool)(凌乱而不是远程便携).我认为写这样的东西要容易得多:
if (someBool)
someBit &= 3;
else if (someOtherBool)
someBit |= 3;
else if (anotherBool)
someBit ^= 3;
all // if all values succeed, run someFunction
someFunction();
else
someOtherFunction();
Run Code Online (Sandbox Code Playgroud)
C有这种能力吗?
可以使用其他变量来完成.例如
int passed = 0;
if (passed = someBool)
{
someBit &= 3;
}
else if (passed = someOtherBool)
{
someBit |= 3;
}
else if (passed = anotherBool)
{
someBit ^= 3;
}
if (passed)
{
someFunction();
}
else
{
someOtherFunction();
}
Run Code Online (Sandbox Code Playgroud)
要阻止GCC显示warning: suggest parenthesis around assignment value,请将每个写 (passed = etc)为((passed = etc)).