PookyFan
PookyFan

Reputation: 840

c++: Operations order in for loop increment part

Consider the following piece of code:

int totalLength = 0;
int partLength = 0;
for(; totalLength < SOME_CONST; totalLength += partLength, partLength = 0)
{
    //partLength may be increased here
}

In this particular case, can I assume that partLength will be set to 0 AFTER it will be added to totalLength (so if partLength will be increased in the loop body, I won't be adding 0 to totalLength at the end of the loop)? I read about c++ sequences and such, but didn't find any clear answer.

Upvotes: 4

Views: 476

Answers (1)

Weak to Enuma Elish
Weak to Enuma Elish

Reputation: 4637

Yes. The left hand side of the comma operator is sequenced before the right hand side. totalLength += partLength will be fully evaluated before it performs partLength = 0.

Upvotes: 5

Related Questions