Latest LQ Deal: Latest LQ Deals
 LinuxQuestions.org c compound conditional
 Programming This forum is for all programming questions. The question does not have to be directly related to Linux and any language is fair game.

 06-04-2005, 12:06 PM #1 steve_96 LQ Newbie   Registered: Jan 2003 Location: VA Distribution: FC 8 Posts: 24 Rep: c compound conditional Can anyone tell me how these statements are evaluated in c: while(a[0] = a[0] + 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
 06-04-2005, 12:13 PM #2 grupoapunte LQ Newbie   Registered: Apr 2005 Posts: 26 Rep: I cant help you with the first one but the second is easy: its like: if (i == k && i > 2 && k > 2);
06-04-2005, 12:23 PM   #3
MoneyCat
LQ Newbie

Registered: Jun 2005
Location: USA
Distribution: Ubuntu 5.04
Posts: 10

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

Code:
```while (a[0] = a[0] + 2)
{
...
...
--k;
}```

Good luck

Last edited by MoneyCat; 06-04-2005 at 04:41 PM.

 06-04-2005, 01:15 PM #4 steve_96 LQ Newbie   Registered: Jan 2003 Location: VA Distribution: FC 8 Posts: 24 Original Poster Rep: 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[337], q, t=1000; main(j) { printf("pi = 3."); while(a[0] += 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"); }
 06-04-2005, 02:14 PM #5 steve_96 LQ Newbie   Registered: Jan 2003 Location: VA Distribution: FC 8 Posts: 24 Original Poster Rep: 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
 06-04-2005, 03:05 PM #6 randyding Member   Registered: May 2004 Posts: 552 Rep: 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[0] = a[0] + 2, --k) is identical to do { a[0]+=2; } while (--k);```
 06-04-2005, 03:39 PM #7 steve_96 LQ Newbie   Registered: Jan 2003 Location: VA Distribution: FC 8 Posts: 24 Original Poster Rep: Thanks all. It finally makes sense (whatever that is). Here's my equivalent code. long k=4000, p, a[337], q, t=1000; main(j) { printf("pi = 3."); while(--k) { a[0] += 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.
 06-04-2005, 04:16 PM #8 randyding Member   Registered: May 2004 Posts: 552 Rep: 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[0]+=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.
 06-04-2005, 04:40 PM #9 MoneyCat LQ Newbie   Registered: Jun 2005 Location: USA Distribution: Ubuntu 5.04 Posts: 10 Rep: 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.
 06-04-2005, 07:17 PM #10 randyding Member   Registered: May 2004 Posts: 552 Rep: 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.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is Off HTML code is Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post la_mafia63 Linux - General 3 07-24-2005 01:02 PM abk4523 Programming 1 03-06-2005 09:50 AM kevin.cowley Programming 1 09-24-2003 03:54 PM j-me Linux - Newbie 3 08-04-2003 02:04 PM bselzler Linux - Networking 0 09-09-2001 09:30 AM

LinuxQuestions.org

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

 Contact Us - Advertising Info - Rules - Privacy - LQ Merchandise - Donations - Contributing Member - LQ Sitemap -