Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > First statement always evaluated first?

Reply
Thread Tools

First statement always evaluated first?

 
 
tconkling
Guest
Posts: n/a
 
      07-09-2005
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

 
Reply With Quote
 
 
 
 
Sensei
Guest
Posts: n/a
 
      07-09-2005
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.
 
Reply With Quote
 
 
 
 
Tim Prince
Guest
Posts: n/a
 
      07-09-2005
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().
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-09-2005
"tconkling" <(E-Mail Removed)> 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_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with ViewState: "function 'ViewState.get_Item' evaluated and returned null" Steph ASP .Net 2 05-11-2011 02:35 PM
Are concatenated conditions (in "if", "for" or "while" loop) ALWAYS completely evaluated? Robin Wenger Java 3 01-20-2011 12:42 AM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
Coolmax CN-550 Network Attached Storage Device Evaluated Silverstrand Front Page News 0 01-03-2006 04:40 AM
Will the second be evaluated if the first is NULL? William Payne C++ 3 04-09-2004 02:53 PM



Advertisments