Path: utzoo!attcan!uunet!cs.utexas.edu!tut.cis.ohio-state.edu!purdue!bu-cs!bloom-beacon!think!barmar From: barmar@think.COM (Barry Margolin) Newsgroups: comp.lang.c Subject: Re: Problem with ()?():() as ending test in for-loop Message-ID: <20658@news.Think.COM> Date: 24 May 89 18:14:28 GMT References: <1200@liszt.kulesat.uucp> Sender: news@Think.COM Reply-To: barmar@kulla.think.com.UUCP (Barry Margolin) Organization: Thinking Machines Corporation, Cambridge, MA Lines: 32 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) although "real C programmers" would probably write it as (n % 10) ? (n < 100) : (k > 2) 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. Barry Margolin Thinking Machines Corp. barmar@think.com {uunet,harvard}!think!barmar