2

So, I was looking at a C code, and saw this condition in a while loop:

while(x&&D(x-1),(x/=2)%2&&(1))

I searched but only found one with commands, and then condition, and not with two conditions.

Naruyoko
  • 153
  • 7
  • 1
    Look in a good C [book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) for a) `while`, b) the && operator, and c) the , (comma) operator. – Phil M Mar 16 '19 at 00:31
  • 2
    The first left argument is unused, only the right one is considered as the condition of the loop. – Salek Mar 16 '19 at 00:35

1 Answers1

1

',' operator in your context evaluates the first condition discards the result and then evaluates the second condition.

x&&D(x-1) this condition is evaluated but the result is not considered. Since in (x/=2)%2&&(1) x value is used the changes made to 'x' in first condition are used in second condition but the truthfulness of first condition is not considered for your while loop.

(x/=2)%2&&(1), here (x/=2) is evaluated as x=x/2. And modulus(%) operator is applied on the result. so say your x value after evaluation of first condition is 11, then x/=2 evaluates x to 5 and then 5%2 is 1(gives reminder of 5/2).

&& is a logical AND operator. If left side of && is true then right side is evaluated. In the above example since left side is true(results to 1) and right side is already 1, your (x/=2)%2&&(1) evaluates to True.

I hope this answers your doubt. Remember with comma operator the left side condition/expression of comma is evaluated but its result is discarded.

you can find little insights to comma operator in below link. https://www.geeksforgeeks.org/comna-in-c-and-c/

arjun jawalkar
  • 138
  • 1
  • 10