質問

int c = someIntegerValue;

// Some code...

int i;
for ( i = 0; i < 5, i < c; i++ ) {
...
}

My compiler says error: expression has no effect, which sounds about right. So, which of those 2 comparisons will be used here? My guess is the i < c is ignored, but I wanted some confirmation from others as I am not in a position to run this code yet...

役に立ちましたか?

解決

The statement

i < 5, i < c

Uses the comma operator, which evaluates all the expressions from left to right but only produces the value of the rightmost one. This means that the expression i < 5 is evaluated and discarded, while the expression i < c is evaluated and actually used by the loop.

I assume that the author meant to write something like this:

i < 5 && i < c

which actually considers both of the expressions.

That said, I'm not sure why this is a compiler error and not a compiler warning. This is legal code, though it's almost certainly a bug. Perhaps you have set the compiler to report errors on warnings?

Hope this helps!

他のヒント

That's a comma operator. It evaluates its left and right operands (always in that order), and yields the result of the right operand. It makes sense to use it if the left operand is evaluated for its side effects; since i < 5 has no side effects, your compiler warns you about it.

It's i < 5, not i < c that's ignored. The code is equivalent to:

for ( i = 0; i < c; i++ ) {
     ...
}

It's difficult to guess what it was intended to be. Perhaps the author actually meant:

for ( i = 0; i < 5 && i < c; i++ ) {
    ...
}

It is i < 5 that is ignored.

The complete expression is:

i < 5, i < c

Which is two separate expressions joined by the comma-operator.

The comma-operator works by evaluating the first expression (i < 5), then the second expression (i < c), and the full expression takes on the value of the second part.

The first part of the expression only matters if it has side-effects.

It is commonly, and correctly used this way:
example

for(i = 0, j = 10; i < 10; ++i, --j)
/* i goes from 0-10, while j goes from 10-0 at the same time */

But in the way shown in your code, it has no meaningful purpose, and only confuses other people.

The result of the comma operator is the value of the right operand.

Here the left operand (i < 5) has no side-effect so

i < 5, i < c

is actually equivalent to

i < c

It's a result of using the comma operator. The left expression is evaluated, and then the result is discarded. After that the right expression is evaluated. The comma operator's result is the result of the right expression.

Since the left expression (i < 5) is a no-op, you get the warning you're seeing. The question now is "what did the author intend?" Without more context, we can't really say, but it's likely one of these:

  i < 5
  i < c
  i < 5 && i < c

Here , comma operator plays a role. Means, it evaulates from left to right and returns the last expression. So i<5 is evaluated, i<c is evaluated but returns i<c result as its the last expression.

i < 5, i < c is completely pointless. It has not difference with i < c.

Depending on authors logic its a mistake.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top