Sign in to follow this  
Endar

Condition testing

Recommended Posts

Endar    668
When I have a condition such as: if( something && another == one_more || something_else ) Will the compiler test 'something', and finding it to be 0, would it stop testing the rest of the condition? So, if it was "if( something && veryLargeLongRunningFunction() )", would the function run every single time?

Share this post


Link to post
Share on other sites
SiCrane    11839
Assuming C++, && and || are short circuit operators. If the first part can determine the resut of the expression, then it won't evaluate the rest of the expression. However, this is only true for the built in && and || operators. If you overload || or && for a type then it will evaluate the whole thing.

Share this post


Link to post
Share on other sites
Jolle    178
It will skip "another == one_more", but will check something_else.

If it was "if( something && veryLargeLongRunningFunction() )", the function would only be run if something was true.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this