if语句检查每个OR操作符
Does an IF-Statement checks every OR operator?
目前我正在用粒子进行测试,有一个重要的问题。
if (condition a || condition b || condition c)
或
if(condition a)
if(condition b)
if(condition c){
}
哪个更快?
c++使用所谓的短路表达式求值,这意味着一旦遇到决定表达式最终结果的项(不管其余项可能求值为什么),它将停止求值项。
因为不管X的值是多少,TRUE OR X
都是TRUE
,所以c++不会计算X。
但是,级联的if
语句是而不是与第一个表达式等效。它相当于具有多个and而不是多个or的表达式。
这个问题可能已经在其他地方回答过了,但是c++使用短路方法,也就是说,如果任何条件通过,其余的都被忽略(在逻辑或:|
的情况下)。
对于逻辑和:&
则相反——第一个条件失败会使if语句短路并提前退出。
下面是一个例子:
if (condition a || condition b || condition c) {
// This code will execute if condition a is true, condition a or b is true, or if all three are true
}
if (condition a && condition b && condition c) {
// This code will only execute if all three are true, but if a is false, it will exit early, the same can be said for b
}
相关文章:
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- Android NDK clang 编译器错误在 Windows 上显示'No such file or directory'
- 模式"allocate memory or use existing data"
- N-API include an .so or dll
- 已解决 - C++ 的崇高文本中的"fatal error: opencv: no such file or directory"
- 将 OR 逻辑运算符从 C++ 转换为 Fortran
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- 我如何将 OR 与 if 语句一起使用字符串
- 将 if 语句中的字符与 or 进行比较
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- "No such file or directory" C++标头问题
- 处理 std::enable_if<...中谓词的逻辑"OR">
- C++ 虽然语句不适用于字符串和"or"
- 我在 #include < bits/stdc++h.> 放置时出错"No such file or directory"
- c++模板OR操作符
- if语句检查每个OR操作符
- 检查IF语句中的多个OR操作符