|| operator question

Started by
4 comments, last by Rayno 20 years, 6 months ago
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]
Advertisement
Correct. This behavior is called "short-circuiting", and is specified by the standard.

How appropriate. You fight like a cow.
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]
Thank you very much guys.

This also proves the | character in valid in thread titles...hmmm.
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.
CoV
If you want all the functions to be called instead of just as many as neccessary, you can use the bitwise or (single |) instead.

This topic is closed to new replies.

Advertisement