Why my for loop works with "&&" and not with ","

Viewed 108

I am stuck at this exercise of c program that have a comma in C for loop if, I replace , with && it works same

for(i = 5, j = i - 1 ; i > 0 , j > 0 ; --i ,j = i - 1)
        printf("\n%d",i);

In this loop I get how for( i = 5,j = i - 1 ; ? ; --i ,j= i - 1) but the part where ? is there I don't get how that is working 1,1 = true ? 1,0 = false ? C is trick that's why love it 3> can you explain me how that part is working

2 Answers

but the part where ? is there i don't get how that is working

The comma operator in C evaluates the expression before the comma, and then the expression after the comma, and then returns the value of the expression after the comma. So the value of the expression 1, 0 in C is 0. The result of 1, 1 is 1. The result of foo(x), bar(x) is the value of bar(x).

This doesn't come up very often because in practice, the comma operator isn't used all that often. It can be handy in a few cases, such as in for or while loops where you might want to manipulate more than one variable each time through the loop. But in general, combining expressions with the comma operator just creates uncertainty about how those expressions will be evaluated, what the result of the overall expression is, and so on. Whenever possible, separate the expressions and execute them one at a time.

Why my for loop works with "&&" and not with ","

The && operator combines (using logical AND) the result of both expressions instead of throwing the result of the first expression away, so depending on the expression you can get a different result than you do with ,. 1 ? 0 and 1 ? 1 give the same result for both , and && because the result of the && depends on the second expression in both cases. But 0 ? 0 and 0 ? 1 will give different results — , again returns the value of the second expression, and && returns 0 because both expressions are considered and 0 AND anything is 0.

; i > 0 , j > 0 ;

Formally, the "," is know as the Comma operator:

The comma operator expression has the form
lhs , rhs
where
lhs - any expression rhs - any expression other than another comma operator (in other words, comma operator's associativity is left-to-right)

First, the left operand, lhs, is evaluated and its result value is discarded. Then, a sequence point takes place, so that all side effects of lhs are complete. Then, the right operand, rhs, is evaluated and its result is returned by the comma operator as a non-lvalue.

More informally, in ;i > 0 , j > 0; the i > 0 is evaluated and ignored to determined if the loop should or not terminate, and then j > 0 is evaluated and its value is used to determined if the loop should (or not) continue.

I am stuck at this exercise of c program that have a comma in C for loop if, I replace , with && it works same

In this case it works the same with "," or "&&" because when i = 1 then j = 0 and consequently j > 0 evaluates to false and you exit the loop. When j > 0 evaluates to false i > 0 also evaluates to false, hence the reason why in your case it is the same using i > 0 && j > 0 or i > 0 , j > 0. Nevertheless, i > 0 && j > 0 and i > 0 , j > 0 are not semantically the same.

In your case:

for(i = 5, j = i - 1 ; i > 0 && j > 0 ; --i ,j = i - 1)

actually, you can simplified i > 0 && j > 0 to just i > 0 since whenever j > 0 evaluates to false i > 0 will also evaluate to false since j = i - 1.

Related