c ++:for循环增量部分中的操作顺序
c++: Operations order in for loop increment part
考虑以下代码:
int totalLength = 0;
int partLength = 0;
for(; totalLength < SOME_CONST; totalLength += partLength, partLength = 0)
{
//partLength may be increased here
}
在这种特殊情况下,我可以假设 partLength 将在添加到 totalLength 之后设置为 0(因此,如果 partLength 将在循环体中增加,我不会在 totalLength 中添加 0循环结束)?我阅读了有关 C++ 序列等的内容,但没有找到任何明确的答案。
是的。逗号运算符的左侧排在右侧之前。 totalLength += partLength
将在执行 partLength = 0
之前进行全面评估。
考虑以下代码:
int totalLength = 0;
int partLength = 0;
for(; totalLength < SOME_CONST; totalLength += partLength, partLength = 0)
{
//partLength may be increased here
}
在这种特殊情况下,我可以假设 partLength 将在添加到 totalLength 之后设置为 0(因此,如果 partLength 将在循环体中增加,我不会在 totalLength 中添加 0循环结束)?我阅读了有关 C++ 序列等的内容,但没有找到任何明确的答案。
是的。逗号运算符的左侧排在右侧之前。 totalLength += partLength
将在执行 partLength = 0
之前进行全面评估。