12

Consider the following (trivial) code segment:

while (i++, i <= 10) {
  // some more code
}

In the general case, C++ allows comma separated statements to be evaluated in any order. In the case of a while loop, are we at least guaranteed (by the specification) that the last statement (which is used as the condition for the loop) be evaluated last?

Felix Fung
  • 1,711
  • 19
  • 27
  • 7
    I suppose this is beside your point, but just in case: You could rewrite that condition as `i++ <= 10` and get rid of the comma operator. Now go and tell me that this is beside your point. `:)` – sbi Nov 01 '10 at 19:59
  • 6
    @sbi - does that do the same thing? I would have expected that with prefix but not postfix. ie. `++i, i<= 10` is the same as `++i <= 10` – Steve Townsend Nov 01 '10 at 20:08
  • @Steve: You got me there. Yes, it should be `++i <= 10`. Sorry for that. (I really shouldn't attempt to do that while [chatting](http://chat.stackoverflow.com/rooms/10/c-lounge).) – sbi Nov 01 '10 at 20:24
  • @sbi - np, I am still hugely in your debt for prior work here. – Steve Townsend Nov 01 '10 at 20:26
  • @sbi - I find a lot of good info in your Stack Overflow answers – Steve Townsend Nov 01 '10 at 20:54

3 Answers3

19

In the general case, C++ allows comma separated statements to be evaluated in any order.

If you're referring to the commas between function arguments, that's just a separator.

In your case, you're using the comma operator, and that introduces a sequence point that guarantees that all side-effects from the comma's left operand have settled down before evaluating the right one.

So yes, it is well-defined.

From section 5.18/1 of the ISO C++98 standard:

A pair of expressions separated by a comma is evaluated left-to-right and the value of the left expression is discarded. The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are not applied to the left expression. All side effects (1.9) of the left expression, except for the destruction of temporaries (12.2), are performed before the evaluation of the right expression. The type and value of the result are the type and value of the right operand; the result is an lvalue if its right operand is.

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
11

Yes. The , operator (unless overloaded!) introduces a so-called sequence point and does indeed guarantee the order of execution from left to right.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
5

The above comments explained it. And one of the common way of abusing this method is

while(scanf("%d", &n), n){
    // do something
}

This will read integer until we read zero.

Timothy Leung
  • 1,407
  • 7
  • 22
  • 39