0

Whats the difference between:

int i;
for( i=0;i<5 && i<3;i++)
{
    printf("stackoverflow.com");
}

and

int i;
for( i=0;i<5 , i<3;i++)
{
    printf("stackoverflow.com");
}

I mean use of && instead of ','

Regards

Sheep
  • 49
  • 1
  • 7
  • 1
    This has been answered. https://stackoverflow.com/questions/54142/how-does-the-comma-operator-work – twain249 Jul 05 '17 at 23:01
  • The suggested duplicate only explains one of the two snippets ... not really a duplicate as such – M.M Jul 05 '17 at 23:29

3 Answers3

1

In the second code block, only i < 3 is actually used to evaluate whether the loop should exit. The i < 5 expression is evaluated, but its value is discarded. See the comma operator for more info.

There is functionally no difference between your examples because i < 3 is the limiting expression and appears second. Both loops will exit when i reaches 3. However, if you switched the terms so that you have i < 3, i < 5, then the second kicks out when it reaches 5 because only the value of i < 5 is considered.

Sean
  • 4,365
  • 1
  • 27
  • 31
0

When using the comma operator , in a statement like the:

( <expression1>, <expression2> )

the <expression1> is evaluated and discarded, then the <expression2> is evaluated and its value is returned. In summary the right most expression is evaluated and its value returned. Other expressions are evaluated and discarded. So your statement of:

for(i=0; i<5 , i<3; i++)

is equivalent to:

for(i=0; i<3; i++)

As for the first statement, this expression:

(i<5 && i<3)

is a simple AND boolean evaluation. A funny one because it is enough to say:

(i<3)

Long story short, who ever made this example probably expects you to say "both conditions evaluate to second expression".

Ron
  • 14,674
  • 4
  • 34
  • 47
  • Just to be precise, the other expressions are evaluated (in left-to-right order) before the rightmost expression. If there are side effects, the order matters. – Barmar Jul 05 '17 at 23:54
  • @Barmar Please clarify. In the case of the comma operator the order matters even without side effects. – klutt Jul 06 '17 at 08:48
  • @klutt If there are no side effects, what difference does order of execution make? As long as it returns the value of the right-hand expression you can't tell which order it executed them. – Barmar Jul 06 '17 at 12:50
  • @Barmar The difference is which expression that counts. In the case of `&&` the order only matters if there are side effects, but with the comma operator you'll get different results regardless. – klutt Jul 06 '17 at 12:54
  • @klutt If you have `a+ 3, b + 4`, what difference does it make what order it does the additions, as long as the result is `b + 4`? – Barmar Jul 06 '17 at 13:10
  • @Barmar `for(i=0; i<5 , i<3; i++)` and `for(i=0; i<3, i<5; i++)` are not equivalent, but neither `i<3` nor `i<5` has side effects. What you first wrote is not logically wrong, but it may sound like you're implying that the order does not matter if there's no side effects. I just wanted everything to be clear. – klutt Jul 06 '17 at 13:13
  • @klutt I meant that the order that they are *executed* matters, not the order that they're *written*. – Barmar Jul 06 '17 at 13:15
  • @Barmar Then we're clear. :) – klutt Jul 06 '17 at 13:16
0

In this case they will do exactly the same.

The thing that differs is that in the first case i<3 will not be evaluated every time. A statement on the form A && B will only execute B if the return value of A is true. The reason is that the logical value of && is true if both operands are true. If one of them are false, the whole expression is false, so if the left operand evaluates to false, there's no need to evaluate the right operand.

The comma operator on the other hand evaluates both operands, but the result of the left operand is discarded.

klutt
  • 30,332
  • 17
  • 55
  • 95