Path: utzoo!attcan!uunet!lll-winken!xanth!ames!apple!rutgers!rochester!kodak!ektools!randolph From: randolph@ektools.UUCP (Gary L. Randolph) Newsgroups: comp.lang.c Subject: Re: Problem with ()?():() as ending test in for-loop Message-ID: <1918@ektools.UUCP> Date: 25 May 89 14:02:25 GMT References: <1200@liszt.kulesat.uucp> <20658@news.Think.COM> <1917@ektools.UUCP> Sender: randolph@ektools (Gary L. Randolph) Reply-To: randolph@ektools.UUCP (Gary L. Randolph) Organization: Eastman Kodak, Dept. 47, Rochester NY Lines: 52 In article <1917@ektools.UUCP> randolph@ektools.UUCP (Gary L. Randolph) writes: >In article <20658@news.Think.COM> barmar@kulla.think.com.UUCP (Barry Margolin) writes: >>In article <1200@liszt.kulesat.uucp> vanpetegem@liszt.kulesat.uucp writes: >>>The code with which I am in trouble, goes as follows: >>> >>> for(n = 1; ((n % 10) ? (k > 2) : (n < 100)); n++) >>> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >>> { >>> ..... (in some expressions k is changed, depending on n) >>> } >>>What I want to do is simply perform the test (k > 2) as ending test for the >>>"for-loop", except in the cases where n is dividable by 10. In the latter >>>situation the test (n < 100) has to be done. > =The sense of the test clause of your :? expression is wrong. When n =is divisible by 10, (n % 10) is 0, and 0 represents "false" in C. I =would write your end test as = ((n % 10) == 0) ? (k > 2) : (n < 100) =i.e. they would just interchange the true and false clauses, taking =advantage of the fact that the result of the % can be used as a =boolean. My original anwer has some problems - PLEASE ignore it... Here is, I believe, a correct answer. NOOOOO! I do NOT think your logic is wrong. I am sure it is correct according to your explanation of what you want to do. There *is* a problem however... ...Stuff here is too wrong (embarrASSing) for me to repeat... You don't mention whether variable k was initialized before entering the for. IT SHOULD BE. I ran a short test as follows: main() { int n; float k = 6; for(n=1;((n % 10) ? (k>2) : (n<100)); n++) { k -= 2; } printf("n: %d\n k: %f\n",n,k); } This produced the expected result: n: 3 k: 2.000000 I hope THIS helps... --Gary Randolph