4

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.

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So it isn't implementation dependent? I was afraid it is, thus that question.
@PookyFan It isn't. You can read more about it here, too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.