473,785 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

First statement always evaluated first?

I have an if statement that looks like this:

if(foo(&x) && x > y)
...

where the value of x is modified by foo, and the comparison between x
and y only makes sense after x has been modified by foo (and, of
course, if foo returns true). Am I guaranteed (assuming my compiler
generates correct code) that x > y is evaluated after foo(&x) returns?

Assuming things work the way I think they do, is it considered bad form
to write code like this? It saves me from doing something like the
following, which I think is ugly-looking:

if(foo(&x))
{
if(x > y)
{
...
}
}

Thanks,
Tim

Nov 15 '05 #1
3 1563
tconkling wrote:
I have an if statement that looks like this:

if(foo(&x) && x > y)
...

where the value of x is modified by foo, and the comparison between x
and y only makes sense after x has been modified by foo (and, of
course, if foo returns true). Am I guaranteed (assuming my compiler
generates correct code) that x > y is evaluated after foo(&x) returns?
Yes in some sense. The order of evaluation is lexicographic, so first

foo(&x)

and *IF* foo is true then is (x > y) evaluated, otherwise it goes rigth
to the else statement.
Assuming things work the way I think they do, is it considered bad form
to write code like this? It saves me from doing something like the
following, which I think is ugly-looking:

if(foo(&x))
{
if(x > y)
{
...
}
}


They are equivalent.
Nov 15 '05 #2
tconkling wrote:
I have an if statement that looks like this:

if(foo(&x) && x > y)
...

where the value of x is modified by foo, and the comparison between x
and y only makes sense after x has been modified by foo (and, of
course, if foo returns true). Am I guaranteed (assuming my compiler
generates correct code) that x > y is evaluated after foo(&x) returns?

Assuming things work the way I think they do, is it considered bad form
to write code like this? It saves me from doing something like the
following, which I think is ugly-looking:

if(foo(&x))
{
if(x > y)
{
...
}
}

Thanks,
Tim

Yes, those forms are equivalent. Of course, you don't need the braces
around the inner if().
Nov 15 '05 #3
"tconkling" <tc*******@gmai l.com> writes:
I have an if statement that looks like this:

if(foo(&x) && x > y)
...

where the value of x is modified by foo, and the comparison between x
and y only makes sense after x has been modified by foo (and, of
course, if foo returns true). Am I guaranteed (assuming my compiler
generates correct code) that x > y is evaluated after foo(&x) returns?

Assuming things work the way I think they do, is it considered bad form
to write code like this? It saves me from doing something like the
following, which I think is ugly-looking:

if(foo(&x))
{
if(x > y)
{
...
}
}


Yes, that's a special property of the "&&" operator (also "||" and
","). The "&&" operator evaluates its left operand first, then
evaluates the right operand only of the left operand evaluted to a
non-zero value. There's also a sequence point between the evaluation
of the left and right operands.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

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

Similar topics

2
1569
by: Sigmund | last post by:
Hi, I would like to put an IF statement within the mail() function. I've tried this and searched around but haven't found anything written about this. Is it possible? I've been doing somethings like : mail ("Email", "Subject", "Body".if() {...}."", "From"); Sorry if this is a dumb question. BUT, if it works I will try similar things in SELECT statements.
28
3597
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a very early draft for a new "assert" syntax: This was inspired in Ruby's assert syntax. I'm not familiar with Ruby at all, so the chances are that this piece of code is broken, but I think the idea is very obvious. In Ruby, assert is simply a...
6
341
by: William Payne | last post by:
Consider the following code: struct foo { char* bar; }; // fooptr is of type foo* if(fooptr != NULL && fooptr->bar != NULL) {
3
1895
by: Roy Adams | last post by:
Hello everyone I'm doing a multiple insert from ten text fields. all named color when I submit the from with the text fields it goes to an asp page with the script to do the job and the go to another page, I've got that bit going but what if the user leaves the fields empty an hits submit? ok a simple enough thing of putting an else in the script, this isn't working!! it always does the first thing I've tried so many different...
21
7655
by: Andy | last post by:
Can someone tell me if the following Switch...Case construct is valid? I'm wanting to check for multiple values in the Case statement without explicitly listing each values. So for example, will case 1-35: work? If this would work, will it consider all numbers between 1 and 35 inclusive of 1 and 35? Please let me know if the following will work below: switch(state){ case 1-35: case 37:
8
404
by: Roy Chastain | last post by:
Question If control leaves the locked block does the lock get cleared? Example (simple and bad coding) lock(obj) { // code return; }
6
2172
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. === PEP: xxx Title: Unification of for-statement and list-comprehension syntax
30
2979
by: lovecreatesbea... | last post by:
K&R says the following in the preface to the first edition, "... the C compiler, and ... are written in C." I'm wondering, does it say even the first / original C compiler was written in C?
22
3178
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point numbers then added support for putting in lat/lon coordinates. I tried this little piece of code inside the OnChar function but compiler complained about missing ';' after "case _T('W'):"
0
9647
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
9485
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10098
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
6743
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();...
0
5390
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...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
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.