(C++) Early Rejection in boolean-expressions

Started by
10 comments, last by jollyjeffers 21 years ago
quote:Original post by superdeveloper

if(GetChar() == 13 && NextCharIsBufferEOF() && IncrementLineNumber(GetChar() == 13)) {
cprintf("New line was reached\n");
};

...or better yet.

if(MovementSuccessful() || UpdateCollisionsSuccess(MyCollisions)) {
DrawShipAndPossibleCollisions(MyCollisions);
};



that''s just called bad programming
Advertisement
quote:that''s just called bad programming

Agreed The nature of AND logic is that all conditions must be TRUE in order for something to happen. If one of those conditions is FALSE, then it''s impossible for the expression to evaluate to TRUE. The compiler uses this information to optimize it''s code with a short-circuit system.

I know what you mean by wanting all your functions to be called, but in that case you shouldn''t call them in an if, where AND or OR logic could short-circuit things. I''ve always prefered this nifty trick:
bool bCheck = true;bCheck &= FirstFunction();bCheck &= SecondFunction();bCheck &= ThirdFunction();if(bCheck){   // Stuff} 

Use an integer if it doesn''t like the boolean.

This topic is closed to new replies.

Advertisement