Archived

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

Using the && operator as a short-curcuit

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

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 on other sites
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 on other sites
Nevermind the syntax you use, in two cases the compiler will produce the same assembly code...

Share on other sites
The problem was that I was using a void function. Thanks.

Sean

Share on other sites
no, you replace things like

  if(x)  doSomething(x);// withif(x && doSomething(x))  ;//orif(x)  if(doSomething(x))    doSomthingElse(x);// withif(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 hereelse  // 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 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 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 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 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 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 ...

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• Forum Statistics

• Total Topics
631419
• Total Posts
2999977
×