Archived

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

Rayno

|| operator question

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
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
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