LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c compound conditional (https://www.linuxquestions.org/questions/programming-9/c-compound-conditional-330307/)

 steve_96 06-04-2005 12:06 PM

c compound conditional

Can anyone tell me how these statements are evaluated in c:
while(a = a + 2, --k)
also,
if (k == j > 2)

(I'm trying to recode this: http://www.boo.net/~jasonp/pigjerr in statements I can understand)
Steve

 grupoapunte 06-04-2005 12:13 PM

I cant help you with the first one but the second is easy:

its like:

if (i == k && i > 2 && k > 2);

 MoneyCat 06-04-2005 12:23 PM

Quote:
 while(a = a + 2, --k)
This is a terrible form of programming which C supports. What this is saying is essenatially this:

Code:

```while (a = a + 2)  {     ...     ...     --k; }```

Good luck:)

 steve_96 06-04-2005 01:15 PM

I don't seem to get either of those to work. Here's my code (with the offending expressions that work, but I don't understand why):

long k=4000, p, a, q, t=1000;
main(j) {
printf("pi = 3.");
while(a += 2,--k) {
j = q = 0;
p = 1 + 2 * k;
while(j < 337) {
if (k == j > 2) {
printf("%.3d", a[j-2] % t + q/p/t);
}
q = a[j] * k + q % p * t;
a[j++] = q/p;
}
}
printf("\n");
}

 steve_96 06-04-2005 02:14 PM

Some progress
if ((k == 1 && j > 2) || (k == 0 && j <= 2))
seems to work as a replacement for
if (k == j > 2)
I guess we're evaluating j > 2 to 0 or 1 and comparing that to k.

Steve

 randyding 06-04-2005 03:05 PM

Yuck!
Don't program that way. Whoever gave you that code should be biXch slapped.

Anyway, when expressions are separated by ',' only the value of the last one is returned for evaluation.
Code:

```while(a = a + 2, --k) is identical to     do {         a+=2;     } while (--k);```

 steve_96 06-04-2005 03:39 PM

Thanks all. It finally makes sense (whatever that is). Here's my equivalent code.

long k=4000, p, a, q, t=1000;
main(j) {
printf("pi = 3.");
while(--k) {
a += 2;
j = q = 0;
p = 1 + 2 * k;
while(j < 337) {
if ((k == 1 && j > 2) || (k == 0 && j <= 2)) {
printf("%.3d", a[j-2] % t + q/p/t);
}
q = a[j] * k + q % p * t;
a[j++] = q / p;
}
}
printf("\n");
}

Randyding, the original author was trying to calculate 1000 digits of pi using the smallest c source he could write -- see link in my original post. I guess he succeeded. I'm trying to understand his algorithm.
What I still don't get is why my interpretation seems to evaluate the original if statement from right to left -- I would have expected left to right.

 randyding 06-04-2005 04:16 PM

Hi, in your equiv. code example I'm not quite sure if the while (--k) { } you're using is equiv. to my do while {} loop. In the way you did it, if k starts as 1 then a+=2 will not be evaluated at all, while in your original post it will be evaluated 1 time.
I would expect different results, but you may have compensated for it in a different place that I don't really see at the moment.

 MoneyCat 06-04-2005 04:40 PM

I remember reading in some C book if anything follows the comma in the while conditional then it is executed as if it was inside the body of while loop.

 randyding 06-04-2005 07:17 PM

Well yes that's partially true, it does execute in the loop. Look at this example.
Code:

```a=3; k=2; while (a+=2, --k) { }```
The code will run until --k is 0 because in a comma separated list the last value is returned for evaluation ... in this case as the while exit condition. --k will be run in the loop every time through, but only after a+=2 is executed because its before --k in the comma separated list. Therefore regardless of k's initial value, a+=2 will always be run at least once.

 All times are GMT -5. The time now is 11:49 PM.