0

Sorry for this easy question but I couldn't find the answer, if I got a while loop that looks like this. Does it calculate before or after the comparison?

a = 0;
while(a++ < 5)
{
    ....

When it first runs the loop will it look at it as "1 < 5" or "0 < 5". Thanks

5
  • 1
    Why don't you try it out? printf('%d', a); in the loop and see what happens. Commented Oct 28, 2014 at 19:14
  • @MarcB: because the result of the print would not be conclusive, and even counter intuitive? Commented Oct 28, 2014 at 19:17
  • @njz: it'd print a as many times as the inner loop executed, which tells you when the while() condition succeeded/failed. Commented Oct 28, 2014 at 19:18
  • Or even better than trying it or asking others: get rid of the ++ entirely and write clean, readable code. Commented Oct 28, 2014 at 19:35
  • Might I suggest a simple online search for 'c operator precedence' Commented Oct 28, 2014 at 20:15

3 Answers 3

2

Comparison is done BEFORE the increment, but the body of the loop sees the value AFTER icrementation. So in the first run you'll compare 0 < 5, but in the loop a will have the value of 1.

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

2 Comments

what if it had said ++a? Would the increment had been done before the comparison?
@Fjodor : Yes - it would then be executed before the compare, so you'd effectively have 1 < 5
1

The postfix operator is applied "after". The prefix operator is applied "before". In the case you've provided the first time 'a' is compared it's value will equal 0

Be careful, cause you can get in trouble with these operators if not used with care. Consider a[ix++] = a[ix] + 1; maybe it will do what you want, maybe not ... try it

Just read the other comment ... very good point that a will take on the ++ value insde the loop.

2 Comments

No matter whether it "works" or not, a[ix++] = a[ix] + 1 is undefined behaviour, and demons might fly out of your nose.
Which may are may not be what you want, depending on how one feels about nose demons :-D
1

The result of the expression a++ is the current value of a, so the loop will start out as while ( 0 < 5 ).

The result of the expression ++a is the value of a + 1, so if you had written while ( ++a < 5 ), it would start out as while ( 1 < 5 ).

In both cases, a will be incremented by 1 as a side effect. Note that the side effect does not have to be applied immediately after the expression is evaluated; the only guarantee is that it is applied before the next sequence point (in this particular case, the sequence point is at the end of the conditional expression, so the body of the loop will see the updated value of a). So, if you have an expression like

x = a++ * ++b;

it will be evaluated as x = a * (b + 1), but there's no guarantee that a will be incremented before ++b has been evaluated, nor is there any guarantee that either will be incremented before the multiplication and the assignment. The following is one of many acceptable order of operations:

  1. t1 <- b + 1
  2. x <- a * t1
  3. b <- b + 1
  4. a <- a + 1

Comments

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.