These two if-statements are equal, right?

W

William Payne

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
 
P

Peter van Merkerk

William Payne said:
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 (*, /, %).
 
I

Ivan Vecerina

Hi William,

| 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,
 
T

tom_usenet

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
 
P

Patrick Kowalzick

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top