Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #12501

Re: Using ternary and summing array

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Using ternary and summing array
Date 2011-11-01 16:36 -0700
Organization None to speak of
Message-ID <lnlirzifea.fsf@nuthaus.mib.org> (permalink)
References <26bf4806-b232-4878-810b-55f3d3926c9f@p20g2000prm.googlegroups.com> <6615b0de-8283-486e-acaf-fa21560ff96d@p14g2000pra.googlegroups.com> <4EAFBDCD.7264@mindspring.com>

Show all headers | View raw


pete <pfiland@mindspring.com> writes:
> flebber wrote:
>> I created a working solution with an if statement but I would still
>> really love to know if I could use the "?" to acheive the same result.
>
>>    if ((i % 3 == 0)||(i % 5 == 0)){
>>      sum += i;
>>     }
>
> If that is truly your goal:
>
> sum += ((i % 3 == 0)||(i % 5 == 0)) ? i : 0;

Yes, but frankly it's a foolish goal.  The "if" form is easier to
read -- though personally I'd find it even easier with more whitespace
and fewer parentheses:

    if (i % 3 == 0 || i % 5 == 0) {
        sum += i;
    }

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using ternary and summing array flebber <flebber.crue@gmail.com> - 2011-10-31 03:35 -0700
  Re: Using ternary and summing array flebber <flebber.crue@gmail.com> - 2011-10-31 04:06 -0700
    Re: Using ternary and summing array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-31 15:06 +0000
    Re: Using ternary and summing array pete <pfiland@mindspring.com> - 2011-11-01 04:37 -0500
      Re: Using ternary and summing array Keith Thompson <kst-u@mib.org> - 2011-11-01 16:36 -0700
  Re: Using ternary and summing array flebber <flebber.crue@gmail.com> - 2011-10-31 04:04 -0700
  Re: Using ternary and summing array Jean-Christophe <5.d@free.fr> - 2011-10-31 04:16 -0700
    Re: Using ternary and summing array Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-10-31 15:00 +0000
    Re: Using ternary and summing array flebber <flebber.crue@gmail.com> - 2011-10-31 16:11 -0700
      Re: Using ternary and summing array James Kuyper <jameskuyper@verizon.net> - 2011-10-31 21:49 -0400
    Re: Using ternary and summing array Kenneth Brody <kenbrody@spamcop.net> - 2011-11-03 12:04 -0400
      Re: Using ternary and summing array Kaz Kylheku <kaz@kylheku.com> - 2011-11-03 17:03 +0000
        Re: Using ternary and summing array Kenneth Brody <kenbrody@spamcop.net> - 2011-11-08 13:08 -0500
  Re: Using ternary and summing array James Kuyper <jameskuyper@verizon.net> - 2011-10-31 07:46 -0400
    Re: Using ternary and summing array David Thompson <dave.thompson2@verizon.net> - 2011-11-21 02:39 -0500

csiph-web