Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Sean99

Using the && operator as a short-curcuit

This topic is 5947 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I thought it was possible to replace: if(x){doSomething(x);} with something like: x && doSomething(x); but my compiler is telling me "void value not ignored as it ought to be". What''s the proper syntax for the second expression above? I tried putting paranthesis around it but that didn''t help. Sean

Share this post


Link to post
Share on other sites
Advertisement
does the dosomething function return a value. If it doesnt, then it wont work. I''m not sure on the proper syntax though, just the return bit.

-J

Share this post


Link to post
Share on other sites
no, you replace things like


  
if(x)
doSomething(x);

// with


if(x && doSomething(x))
;
//or


if(x)
if(doSomething(x))
doSomthingElse(x);

// with


if(x && doSomething(x)
doSomethingElse(x);


using a C behavior called short-circuit evaluation, which means that the C compiler evaluates the elements of a compound logical expression from left-to-right, and stops evaluating at the first case where the final outcome becomes known...

the main place this is used is so you can do this type of thing:


  
if(x != NULL && x->DoSomething())
// do succeeded stuff here

else
// do failure stuff here ...



this way you don''t need to nest the NULL check and the actual function call into extra levels ... cause the function call will never happen if the null check fails.

Share this post


Link to post
Share on other sites
I may be incorrect, but I believe that the order of evaluation is not garaunteed so you might want to be careful about using it like you want to.

Karg

Share this post


Link to post
Share on other sites
quote:
Original post by Karg
I may be incorrect, but I believe that the order of evaluation is not garaunteed so you might want to be careful about using it like you want to.

Karg


It is guaranteed to be left-to-right for &&, ||, the comma operator, and ?:. i.e. they have sequence points inserted.


Share this post


Link to post
Share on other sites
Xai, the replacement I presented in my original post does work (as long as doSomething returns non-NULL). The short-circuiting behavior of && is not limited to "if" expressions.

(Chances are this will already be pointed out by the time I''m done collecting 500 errors.)

Sean

Share this post


Link to post
Share on other sites
I would suggest not using the "short circuit". It obfuscates the intent of the code, and makes it less readable (wait.. that''s redundant... oh well)

Share this post


Link to post
Share on other sites
it is perfectly comon and readable to do a test for NULL (0) on the same line as the use of the newly verified non-null pointer. Most other uses of short-circuit evaluation ARE strange ... especially when you see stuff like this.

DoOriginalJob() || DoThisIfFirstJobFailed()

very hard to understand that a true from job 1 stops evaluation

and this

DoJobOne() && DoNextJob() && DoFinalJob()

it''s really not so obviious that the final job happen ONLY if both other jobs return true ...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!