These two if-statements are equal, right?

Discussion in 'C++' started by William Payne, Sep 9, 2003.

  1. Hi, I was going through some old code of mine and spotted this:

    if(i == 16 || i == 32 || i == 48 || i == 64 || i == 80 || i == 96 || i ==
    112 || i == 128 ||
    i == 144 || i == 160 || i == 176 || i == 192 || i == 208 || i == 224 || i
    == 240)

    i is of type int.

    Now, isn't that if-statement equal to:
    if(i >= 16 && i <= 240 && i % 16 == 0) ?

    Seems to be equal, but I just wanted to hear someone say yes (or no). Which
    variant do you prefer if they are equal?

    // William Payne
    William Payne, Sep 9, 2003
    #1
    1. Advertising

  2. "William Payne" <> wrote in message
    news:bjkc0a$l8h$...
    > Hi, I was going through some old code of mine and spotted this:
    >
    > if(i == 16 || i == 32 || i == 48 || i == 64 || i == 80 || i == 96 || i

    ==
    > 112 || i == 128 ||
    > i == 144 || i == 160 || i == 176 || i == 192 || i == 208 || i ==

    224 || i
    > == 240)
    >
    > i is of type int.
    >
    > Now, isn't that if-statement equal to:
    > if(i >= 16 && i <= 240 && i % 16 == 0) ?


    If you ask me both versions will produce the same result.

    > Seems to be equal, but I just wanted to hear someone say yes (or no).

    Which
    > variant do you prefer if they are equal?


    Depends on the purpose of the code. If being divisible by 16 has a
    special meaning in this context I would prefer the latter one. If the
    numbers to be tested only accidentally happen to be divisible by 16 I
    would prefer first one. If it involves bit testing and/or manipulation I
    personally prefer using the 'and' operator (&), the 'or' operator (|)
    and the shift operators (<<, >>), over arithmetric operators (*, /, %).

    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
    Peter van Merkerk, Sep 9, 2003
    #2
    1. Advertising

  3. Hi William,

    "William Payne" <> wrote in message
    news:bjkc0a$l8h$...
    | Hi, I was going through some old code of mine and spotted this:
    |
    | if(i == 16 || i == 32 || i == 48 || i == 64 || i == 80 || i == 96 || i ==
    | 112 || i == 128 ||
    | i == 144 || i == 160 || i == 176 || i == 192 || i == 208 || i == 224 ||
    i
    | == 240)
    |
    | i is of type int.
    |
    | Now, isn't that if-statement equal to:
    | if(i >= 16 && i <= 240 && i % 16 == 0) ?
    |
    | Seems to be equal, but I just wanted to hear someone say yes (or no).
    | Which variant do you prefer if they are equal?

    Yes.
    The second one, definitely.
    (criterion: less clutter = less opportunity to mis-type or mis-read).

    Alternatively, if the test could not be simplified mathematically,
    I would use:
    switch(i) {
    case 16: case 32: case 48: case 64:
    case 80: case 96: case 112: case 128:
    case 144: case 160: case 176: case 192:
    case 208: case 224: case 240:
    doA;
    }

    Because a typos like the 3 following ones may not be obvious:
    if(i == 16 || i== 32 || i == 48 || i == 64 || i == 80
    || i == 96 || i =112 || i == 128 ||
    i == 144 || 1 == 160 || i == 176 || l==192 || i == 208 || i == 224 || i
    == 240)


    hth,
    --
    http://www.post1.com/~ivec <> Ivan Vecerina
    http://www.brainbench.com <> Brainbench MVP for C++
    Ivan Vecerina, Sep 9, 2003
    #3
  4. William Payne

    tom_usenet Guest

    On Tue, 9 Sep 2003 13:03:18 +0200, "William Payne"
    <> wrote:

    >Hi, I was going through some old code of mine and spotted this:
    >
    >if(i == 16 || i == 32 || i == 48 || i == 64 || i == 80 || i == 96 || i ==
    >112 || i == 128 ||
    > i == 144 || i == 160 || i == 176 || i == 192 || i == 208 || i == 224 || i
    >== 240)
    >
    >i is of type int.
    >
    >Now, isn't that if-statement equal to:
    >if(i >= 16 && i <= 240 && i % 16 == 0) ?


    Yup, looks like it (without carefully checking the individual
    numbers).

    >Seems to be equal, but I just wanted to hear someone say yes (or no). Which
    >variant do you prefer if they are equal?


    The second is much shorter and therefore clearer. The compiler should
    be able to optimize it since it uses integral literals (%16 is
    equivalent to a simple bitwise and).

    Tom
    tom_usenet, Sep 9, 2003
    #4
  5. > Now, isn't that if-statement equal to:
    > if(i >= 16 && i <= 240 && i % 16 == 0) ?
    >
    > Seems to be equal, but I just wanted to hear someone say yes (or no).

    Which
    > variant do you prefer if they are equal?


    I would do it like this ;-) :

    if( i % 16 == 0 && i <= 240 && i >= 16 )

    Patrick
    Patrick Kowalzick, Sep 9, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank
    Replies:
    5
    Views:
    476
    Rob Dekker
    Feb 3, 2006
  2. Dave56
    Replies:
    1
    Views:
    476
    Robbe Morris [C# MVP]
    Jul 29, 2005
  3. Oxnard
    Replies:
    4
    Views:
    341
    Joona I Palaste
    Oct 26, 2004
  4. Replies:
    2
    Views:
    329
  5. mahaa_hth
    Replies:
    1
    Views:
    1,374
    mahaa_hth
    Oct 22, 2008
Loading...

Share This Page