首页 > 解决方案 > 对于所有编译器,C++ 中的 If 语句中的各种条件是否总是有相同的执行顺序?

问题描述

让我们假设一个带有两个条件 A 和 B 的简单 If 语句:

If ( condA && condB) 

所有编译器的序列都相同吗?

  1. 条件A
  2. 条件 B

如果条件 A 已经为假,那么条件 B 的执行是否是可选的?

标签: c++if-statementmultiple-conditions

解决方案


Yes. Not evaluating condition B if A is false is called short circuit logic, and this behavior is guaranteed by the language specification.


推荐阅读