Hi William,
"William Payne" <mi************ **@student.liu. se> wrote in message
news:bj******** **@news.island. liu.se...
| 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++