Skip to main content
September 24, 2008
Question

Is Odd or Even Function

  • September 24, 2008
  • 6 replies
  • 5096 views
Is there a way to take a number from the database and determine weather or not it is odd or even?

I am listing a number of employees and I want to alternate the background color of the cells of the table housing their names on a webpage.
    This topic has been closed for replies.

    6 replies

    Inspiring
    September 24, 2008
    Adam Cameron wrote:
    > Just a note to Azadi, Kronin555 & Ian:
    >
    > A "MOD 2" operation already resolves to a boolean, so the further
    > comparison to 0 or 1 is not necessary.
    >

    To be precise... since it is lunch time on the Pacific coast... I am
    very hungry... I only have an Maruchan 'Instant Lunch' cup of noodles to
    eat... And thus I am in a bit of an argumentative mood...

    Thanks to ColdFusion typeless nature the results of a "MOD 2" operation
    can be reliably converted to a boolean.

    But some do not care to code with this implicit conversion so they like
    to be explicit with their comparisons.

    Also, as I *KNOW* you are fully aware of Adam, it could be confusing to
    somebody who has not been exposed to the MOD operator before and thus
    may not be clear on what it does. Thus such person should really read
    the documentation on what MOD does! ;-)
    Participating Frequently
    September 24, 2008
    quote:


    Thanks to ColdFusion typeless nature the results of a "MOD 2" operation
    can be reliably converted to a boolean.

    But some do not care to code with this implicit conversion so they like
    to be explicit with their comparisons.



    Actually, CF will let you treat any integer as a boolean. Any non-zero number is True. Zero is false.
    <cfoutput>
    <cfloop from="-10" to="10" index="idx">
    #idx#: <cfif idx>True<cfelse>False</cfif><br>
    </cfloop>
    </cfoutput>

    This lets you do some interesting things:
    <cfset reallytrue = true + true>
    #reallytrue#
    <cfset maybetrue = false + true>
    #maybetrue#

    Explicitly testing for mod 2 eq 0 is, in my mind, much clearer, easier for other coders to understand, and thus more beneficial.

    <cfif intval mod 2>
    What values get into this if?
    <cfif intval mod 2 eq 0>
    At least here, we're told, in the code, that any values divisible by 2 with no remainder will pass this if test.

    Coldfusion's typeless nature is nice sometimes, but it's always better to be explicit, in my opinion.
    Inspiring
    September 24, 2008
    Just a note to Azadi, Kronin555 & Ian:

    A "MOD 2" operation already resolves to a boolean, so the further
    comparison to 0 or 1 is not necessary.

    {code}
    <cfif i mod 2>
    odd
    <cfelse>
    even
    </cfif>
    {code}

    --
    Adam
    Inspiring
    September 24, 2008
    PopcornCoder wrote:
    > Is there a way to take a number from the database and determine weather or not
    > it is odd or even?
    >
    > I am listing a number of employees and I want to alternate the background
    > color of the cells of the table housing their names on a webpage.
    >

    The mod operator can be used for this.

    <cfif aValue MOD 2 eq 1>
    aValue is ODD
    <cfelse>
    aValue is EVEN
    </cfelse>

    OR

    <cfif aValue MOD 2 eq 0>
    aValue is EVEN
    <cfelse>
    aValue is ODD
    </cfelse>
    Inspiring
    September 24, 2008
    <cfoutput query="yourquery">

    <tr bgcolor="#iif(CurrentRow mod 2, de(colour1), de(colour2))#">

    Participating Frequently
    September 24, 2008
    <cfif number mod 2 eq 0>
    even
    <cfelse>
    odd
    </cfif>
    Inspiring
    September 24, 2008
    use MOD operator:
    <cfif someintegervalue MOD 2 eq 0>
    even
    <cfelse>
    odd
    </cfif>

    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com/