473,725 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on "if" statements...

Hello all,

I was wondering if there were some sort of limitations on the "if"
statement? I'm writing a program which needs to check a bunch of
conditions all at the same time (basically). And I'm pretty sure the
rest of the program is working just fine. The only thing I could think
might be wrong is that the if statement can only hold so many values in
itself? Let me show what I'm doing:

if (table001[n[0]][x[0]>>5]&b[x[0]&0x1f] != 0 &&
table002[n[1]][x[1]>>5]&b[x[1]&0x1f] != 0 &&
table003[n[2]][x[2]>>5]&b[x[2]&0x1f] != 0 &&
... &&
table030[n[29]][x[29]>>5]&b[x[29]&0x1f] != 0)
{
do_stuff();
}//end if
else do_other_stuff( );

My problem is that "do_stuff" is never executed. There are 30 different
tables of int's I'm checking against up there. I could use more in the
future, and for the purposes of my program, I don't want to use less. I
know that do_stuff should be executed because I have run test cases
where it should have been run. But it just keeps goin to the "else"
statement. I'm probably way off base thinking C is the problem. If you
want to know about my setup here it is:
WinXP, P4 1.8, Compiler: DJGPP using gcc v3.0.3

So, am I expecting too much out of the if statement? Am I using the if
statement incorrectly up there? Any advice or help in this regard is
greatly appreciated.

-David C.
Nov 14 '05 #1
35 2728

On Mon, 26 Apr 2004, David Cleaver wrote:

I was wondering if there were some sort of limitations on the "if"
statement? I'm writing a program which needs to check a bunch of
conditions all at the same time (basically). And I'm pretty sure the
rest of the program is working just fine. The only thing I could think
might be wrong is that the if statement can only hold so many values in
itself?
There is some limitation given by the official standard in that
it specifies a "minimal limit" for the depth of expressions (if I
recall correctly). That is, the Standard says how many "ands" an
'if' statement *must* be able to hold; but any decent implementation
will be able to handle as many as you can throw at it (up to the
available memory on your machine).
Let me show what I'm doing:

if (table001[n[0]][x[0]>>5]&b[x[0]&0x1f] != 0 &&
table002[n[1]][x[1]>>5]&b[x[1]&0x1f] != 0 &&
table003[n[2]][x[2]>>5]&b[x[2]&0x1f] != 0 &&
... &&
table030[n[29]][x[29]>>5]&b[x[29]&0x1f] != 0)
{
do_stuff();
}//end if
else do_other_stuff( );

My problem is that "do_stuff" is never executed.


Have you considered the possibility that the body of the 'if' is
never executed because the big complicated condition is, in fact,
*false*? Have you tried running the 'if' statement in a program
in which "table001.. 030" and "b" are entirely filled with 0x1f
values? What values are stored in those tables during the run of
your current program?

[OT: And why do you need so many tables anyway, and why not use
a "for" loop to check all of them, rather than hard-code the
number of tables into your program?]

-Arthur
Nov 14 '05 #2
David Cleaver wrote:
Let me show what I'm doing:

if (table001[n[0]][x[0]>>5]&b[x[0]&0x1f] != 0 &&

[...]

The formatting is a bit misleading. This is parsed as:

if (table001[n[0]][x[0]>>5] & (b[x[0]&0x1f] != 0) &&
....

Ralf
Nov 14 '05 #3

"David Cleaver" <wr*****@morphe us.net> wrote in message
news:40******** *******@morpheu s.net...

if (table001[n[0]][x[0]>>5]&b[x[0]&0x1f] != 0 &&
table002[n[1]][x[1]>>5]&b[x[1]&0x1f] != 0 &&
table003[n[2]][x[2]>>5]&b[x[2]&0x1f] != 0 &&
... &&
table030[n[29]][x[29]>>5]&b[x[29]&0x1f] != 0)
{
do_stuff();
}//end if
else do_other_stuff( );


Are you REALLY sure that this is the test you want to be performing?

You have a really long expression containing three different operators (&,
!=, &&) used many times each. Since you have not told the compiler how to
group them, you are basically choosing to let the compiler make the decision
for you. This is frequently not a good idea.



Nov 14 '05 #4
"David Cleaver" <wr*****@morphe us.net> wrote in message
news:40******** *******@morpheu s.net...
I was wondering if there were some sort of limitations on the "if"
statement? I'm writing a program which needs to check a bunch of
conditions all at the same time (basically). And I'm pretty sure the
rest of the program is working just fine. The only thing I could think
might be wrong is that the if statement can only hold so many values in
itself? Let me show what I'm doing:

if (table001[n[0]][x[0]>>5]&b[x[0]&0x1f] != 0 &&
table002[n[1]][x[1]>>5]&b[x[1]&0x1f] != 0 &&
table003[n[2]][x[2]>>5]&b[x[2]&0x1f] != 0 &&
... &&
table030[n[29]][x[29]>>5]&b[x[29]&0x1f] != 0)
{
do_stuff();
}//end if
else do_other_stuff( );


First, the order of operations isn't clear in your expression. As a rule of
thumb, if the reader needs to look up the rules to figure out what an
expression does, you should add parens to make it clear what you intended.
Second, judicious use of spaces makes complex expressions more readable.
Third, the test against zero is unnecessary since that's implied with
logical operators.

if ( ( table001[n[0]][x[0]>>5] & b[x[0]&0x1f] ) &&
( table002[n[1]][x[1]>>5] & b[x[1]&0x1f] ) &&
( table003[n[2]][x[2]>>5] & b[x[2]&0x1f] ) &&
... &&
( table030[n[29]][x[29]>>5] & b[x[29]&0x1f] ) )
{
do_stuff();
}//end if
else do_other_stuff( );

I don't know if this code is any more correct, but it's a lot easier to read
and debug, which others (and you, later on) will appreciate.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #5
William L. Bahn wrote:

"David Cleaver" <wr*****@morphe us.net> wrote in message
news:40******** *******@morpheu s.net...

if (table001[n[0]][x[0]>>5]&b[x[0]&0x1f] != 0 &&
table002[n[1]][x[1]>>5]&b[x[1]&0x1f] != 0 &&
table003[n[2]][x[2]>>5]&b[x[2]&0x1f] != 0 &&
... &&
table030[n[29]][x[29]>>5]&b[x[29]&0x1f] != 0)
{
do_stuff();
}//end if
else do_other_stuff( );


Are you REALLY sure that this is the test you want to be performing?

You have a really long expression containing three different operators (&,
!=, &&) used many times each. Since you have not told the compiler how to
group them, you are basically choosing to let the compiler make the
decision for you.


The OP is have the *language* make the decision for them - C has a
defined "order of precedence" [1]. Having lots of conditions separated
by && should be no problem at all.

On the other hand, I'm pretty sure [2] that "X & Y != 0", applying that
order of precedence, groups as "X & (Y != 0)". <fx:flappingPag es/> Yes.

In this case, I'd just write the expression as "X & Y". [Well, actually,
I wouldn't write a monster expression with 30 lines in it embedded in
an if-expression at all, but that's a separate issue.]

[1] defined implicitly by the grammar in the Standard, but expressible
as precedence order.

[2] certain.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #6
Chris Dollin <ke**@hpl.hp.co m> wrote:
On the other hand, I'm pretty sure [2] that "X & Y != 0", applying that
order of precedence, groups as "X & (Y != 0)". <fx:flappingPag es/> Yes.

In this case, I'd just write the expression as "X & Y".


No, you wouldn't; those two expressions can have different values if Y
is non-zero, but not equal to 1. For example, if X==3 and Y==2, the
first expression has the value 1, and the second the value 2. More
importantly to the OP, if X==Y==2, the second expression still has the
value 2, but the first expression now is 0...

Richard
Nov 14 '05 #7
Richard Bos wrote:
Chris Dollin <ke**@hpl.hp.co m> wrote:
On the other hand, I'm pretty sure [2] that "X & Y != 0", applying that
order of precedence, groups as "X & (Y != 0)". <fx:flappingPag es/> Yes.

In this case, I'd just write the expression as "X & Y".


No, you wouldn't; those two expressions can have different values if Y
is non-zero, but not equal to 1. For example, if X==3 and Y==2, the
first expression has the value 1, and the second the value 2. More
importantly to the OP, if X==Y==2, the second expression still has the
value 2, but the first expression now is 0...


That's the point - I believe that the OP wrote "X & Y != 0" and *meant*
"(X & Y) != 0", which can be written as "X & Y" in conditional context
with the same effect, where it avoids all questions of the relative
precedence of & and !=. Hence "in this case".

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #8
Chris Dollin (== me) wrote:
The OP is have the *language* make the decision for them - C has a
defined "order of precedence" [1]. Having lots of conditions separated
by && should be no problem at all.


Erm, in my haste I did not make it clear that one should still
write expressions for clarity - there's a difference between knowing
the C precedence levels, assuming the reader will know them, and
writing for the IOCC.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 14 '05 #9


Chris Dollin wrote:

William L. Bahn wrote:

David Cleaver wrote:

if (table001[n[0]][x[0]>>5]&b[x[0]&0x1f] != 0 &&
table002[n[1]][x[1]>>5]&b[x[1]&0x1f] != 0 &&
table003[n[2]][x[2]>>5]&b[x[2]&0x1f] != 0 &&
... &&
table030[n[29]][x[29]>>5]&b[x[29]&0x1f] != 0)
{
do_stuff();
}//end if
else do_other_stuff( );

Are you REALLY sure that this is the test you want to be performing?

You have a really long expression containing three different operators (&,
!=, &&) used many times each. Since you have not told the compiler how to
group them, you are basically choosing to let the compiler make the
decision for you.


The OP is have the *language* make the decision for them - C has a
defined "order of precedence" [1]. Having lots of conditions separated
by && should be no problem at all.

On the other hand, I'm pretty sure [2] that "X & Y != 0", applying that
order of precedence, groups as "X & (Y != 0)". <fx:flappingPag es/> Yes.

In this case, I'd just write the expression as "X & Y". [Well, actually,
I wouldn't write a monster expression with 30 lines in it embedded in
an if-expression at all, but that's a separate issue.]


Thank you very much. I never thought about there being a precedence
issue. I've fixed my code to better reflect what I was doing. [And I
was wanting (x & y), not x & (y != 0), I've changed it all to be of the
form (x & y) && ...]

May I ask, without making those tables 3-dimensional, how could I make
that code segment look better? Someone earlier in this thread mentioned
putting it into a for loop, but I can't see how that'd be possible with
30 different tables.

Anyway, I wanted to thank everyone for their suggestions so far. I'm
really glad a place like this exists where people can come for help.
Thanks again.

-David C.

[1] defined implicitly by the grammar in the Standard, but expressible
as precedence order.

[2] certain.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html

Nov 14 '05 #10

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

Similar topics

5
1674
by: Jack | last post by:
I'm setting up a form and want to check if some conditions are met when the form submits. If they aren't, the form displays a missing field message and the user has to go back to fill in the missing data. If it is met, the form continues processing. I have the $state variable coming from a drop down SELECT option with "" being the SELECTED default. The if statements are included inside the form tags on the form page, not on the...
40
3035
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the goodness of Python whenever I talk with fellow developers, but I always hit a snag when it comes to discussing the finer points of the execution model (specifically, exceptions). Without fail, when I start talking with some of the "old-timers"...
3
1589
by: sapnsapn | last post by:
Hi folks, I am looking at an application written in C some 10+ years ago. There is a lot of such if statements: if (my_number) {....} else {...} Here, my_number is an unsigned int. What exactly is the "if" looking for to evaluate? Is it the existence of a numerical value or a non-zero value or just any positive value? I am not a C programmer by trade and hence this question.
3
29785
by: Mark Sullivan | last post by:
When I trace through a csharp program I came to a situation where a certain values has an "undefined value" as shown in the debugger DbgClr. I want to check this but the following statements did not recognize this "non"-value if (type.Particle != Undefined.Value) { .... or if (type.Particle != null) { .....
7
1888
by: Mike Heard | last post by:
Hi. I have a question about the evaluation of IF statements when the && operator is used. I have been under the assumption that if the first expression in an IF statement is FALSE it will not evaluate anything after the && in the same statement. I have some code that is similar to this: PaymentEntity pe = GetPaymentEntity(); if (pe.Type == PaymentType.CreditCard &&
22
2255
by: Technoid | last post by:
Is it possible to have a conditional if structure nested inside a conditional switch structure? switch(freq) { case 1: CASENAME if (variable==1) { do some code }
12
20696
by: John | last post by:
I can't get my head around this! I have the following code: <% .... Code for connection to the database ... .... Code for retrieving recordset ... If Not rs.EOF Then ... Do something...
3
1630
Lazandra
by: Lazandra | last post by:
I have a form, and if credit card details have been entered i want it to save in the field completed a "c". However if it doesnt have the credit card details i want it to save to the field completed a "n". I have done code for it. But can you have two "if" statements in one query? Heres my code: if ($submit == "Save" || $payment_cc != "") { $saved = mysql("db69117798","insert into payment values (0, '$infoid', '$product_dogmobile',...
8
2954
by: =?Utf-8?B?UGFvbG8=?= | last post by:
Is there any way I can avoid the ugly and repetitive 'if' construct shown below? I can't refactor into a separate method because qryTrans is an anonymous type. I don't think I can use a switch statement either. It works fine but is not aesthetically pleasing! private void btnSearch_Click(object sender, EventArgs e)
0
8888
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
9257
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
9174
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
9111
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...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
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
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2634
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2157
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.