# These two if-statements are equal, right?

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

1. ### William PayneGuest

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

2. ### Peter van MerkerkGuest

"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

3. ### Ivan VecerinaGuest

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
4. ### tom_usenetGuest

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
5. ### Patrick KowalzickGuest

> 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