473,654 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

These two if-statements are equal, right?

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
Jul 19 '05 #1
5 3291

"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) ?
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.merke rk(at)dse.nl
Jul 19 '05 #2
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++
Jul 19 '05 #3
On Tue, 9 Sep 2003 13:03:18 +0200, "William Payne"
<mi************ **@student.liu. se> 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
Jul 19 '05 #4
> 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
Jul 19 '05 #5
MCWarrior76
2 New Member
Yea, I like the 2nd one thats probly how i'd write it, hey i got a question for anyone that can help......im writin a game program for my nephew, and came across a problem, I wrote,
if((TR=='a')&&( dresser_pick==' b')) || ((TR=='a')&&(dr esser_pick2=='b '))
it says i need a ; b4 the || but i no thats not rite, can anyone help, i dont think the syntax is wrong but mayb it is and jus forgettin somethin

THX, ~Andrew~
Jul 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1391
by: Jack | last post by:
Hi, I'm looking for a RELIABLE Activex button to use in my app developed in VB6 and for now I found these 2 buttons: SMButton v2.5 from www.station-media.com STFButton/ATL v1.5 from www.windowscontrols.com Is there anyone using these buttons who can give me some feedback
2
1470
by: Dilip | last post by:
Gurus I didn't personally encounter these problems but some friends of mine who are doing some deep heavy-metal C#/Networking application have run into these problems. Does anybody have any idea if these are addressed for a future release? thanks --Dilip
1
2345
by: Ersin Gençtürk | last post by:
We couldn't find why these errors happen.They doesn't appear everytime.And they appear different pages at different times.Is there somebody know why these happenes ? First user gets this error : Server : WEB System.Web.HttpUnhandledException: Exception of type System.Web.HttpUnhandledException was thrown. ---> System.NullReferenceException: Object reference not set to an instance of an
1
1998
by: Dino Vliet | last post by:
Hi folks, I'm busy with analyzing some data and therefore will have to store 2 big tables containing 50 million obs & 25 million obs. Selecting some interesting obs can decrease these tables with maybe a factor 10 but eventually I will have to combine them using a join. These tables contain almost 20 colums and after selecting the appropriate ones I will have maybe 10 colums.
10
2665
by: Mike Cox | last post by:
Hello. My name is Mike Cox. I am in charge of the changing of these postgresql lists. I have decided that we are going to drop most of the lists from the vote. We will only be making 4 lists into real Usenet newsgroups if we win the election. The rest of the lists are crap and they take up too much fucking room. Marc was an asshole for stealing BIG8 newsgroup names without voting. He is an irresponsible piece of shit scumbag cock-licking...
3
1745
by: tony | last post by:
Hello!! I use VS 2003 and C# for all class library except MeltPracCommon.dll which is C++.NET The problem is that I get these warnings when building the exe file and use my class libraries. See below for a detail description. Preparing resources...
10
1687
by: @sh | last post by:
Always wondered, now I have the time to ask - we currently backup ALL databases and these seem to all grow in size, the MSDB BAK files are now 35mb each and growing!!! Can we remove these from our Enterprise Manager? - Master - Model - MSDB Cheers, @sh
6
1899
by: pete142 | last post by:
When I compile this code: typedef unsigned char BYTE; BYTE * IpString(unsigned int ip) { static BYTE ipString; ipString = (BYTE) 0xff & (ip >24); ipString = (BYTE) 0xff & (ip >16);
17
12455
by: chewie54 | last post by:
Hello, As an electronics engineer I use some very expensive EDA CAD tool programs that are scriptable using Tcl. I was wondering why these companies have choose to use Tcl instead of Python. Some of these are: Mentor Graphics ModelTech VHDL and Verilog simulator Synopsys Design Compiler and Primetime Static Timing Analyzer Actel FPGA tools.
11
1397
by: Erich | last post by:
Hello all, Today I found myself once again defining two functions that I use all the time: nsplit and iterable. These little helper functions of mine get used all the time when I work. Im sick of having to define them (but am very good at it these days, less than 1 typo per function!). It leads me to the following questions 1. Is this functionality already built in and im just missing it 2. Is there some well known, good technique...
0
8379
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8494
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8596
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.