• Advertisement

Archived

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

|| operator question

This topic is 5250 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

This is pretty newbish of me, but I'm not positive. If I have something like this: if( Func1() || Func2() || Func3() ) { .... Or even the same function with different parameters. If Func1 or Func2 return true, it will go straight to the body of the if statement without calling the proceeding functions, correct? I thought this is true, but I better ask. It could screw up the code I am working on otherwise. [edited by - Rayno on October 8, 2003 2:16:38 AM]

Share this post


Link to post
Share on other sites
Advertisement
Correct. This behavior is called "short-circuiting", and is specified by the standard.


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
Yup. It's called Short Circuit Evaluation (or something to that affect). I'm not sure if it's part of the standard or not (in other word's im not sure if its GUARANTEED to work)... but as far as I know, all compilers do it.

[edited by - tibre on October 8, 2003 2:23:44 AM]

Share this post


Link to post
Share on other sites
Thank you very much guys.

This also proves the | character in valid in thread titles...hmmm.

Share this post


Link to post
Share on other sites
As Sneftel says, it is part of the standard, both of C and C++.

Just to make sure you''re not confused: if Func1 returns true, then neither Func2 nor Func3 will be called, otherwise it behaves exactly as you suggested.

Share this post


Link to post
Share on other sites
If you want all the functions to be called instead of just as many as neccessary, you can use the bitwise or (single |) instead.

Share this post


Link to post
Share on other sites

  • Advertisement