Advertisement

Simple question about IFs, ANDs, ORs

Started by March 06, 2001 05:54 PM
5 comments, last by paulcoz 23 years, 10 months ago
Can someone tell me if an IF statement checks a group of conditions individually and breaks from the statement intelligently? If it does then it would surely be better to phrase this line (which has to evaluate every AND): if (this && this && this && this && this) { // code for first condition } like this (which quits after the first condition is FALSE): if (this) { if (this) { if (this) { // code for first condition } } } If that's the case, then what about this statement: if (!this || !this || !this || !this || !this) { // code for other condition } How could you optimise this, if the IF does not quit after the first !this test fails? Which way does it work? Paulcoz. Edited by - paulcoz on March 6, 2001 7:04:41 PM
A decent compiler will drop out as soon as possible (VC++ 6 does). It also depends on the programming language, I think JAVA is specified to drop out as early as poss.
Gee Brain, what we gonna do tonight?
Advertisement
BReaks at the first False.
I was influenced by the Ghetto you ruined.
AND breaks at the first false as said above. I believe they are evaluated left to right, so put your least expensive tests first.

OR breaks at the first true. So, once again, I believe you should place your least expensive tests first.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
It''s true that they''re evaluated left-to-right, which allows you to do things like:

if( pObject && pObject->IsReady() )
{
}



IsReady() isn''t called if pObject is NULL.

~CGameProgrammer( );


~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
While reading the Topic "Simple question about IFs, ANDs, ORs" I discovered this weird quirk of C++ 9or maybe just the compiler)

  using namespace std;class Object{	public:	    bool IsReady() { return true; }};int main(){	Object* p = NULL;	if(!p && p->IsReady())	{	}	return 0;}  


p->IsReady() should cause an access violation because p is a pointer to NULL. Unfortunately (or fortunately?) because Object''s IsReady method is declared inline, p->IsReady() is replaced by the compiler with the inline code.

Should this be considered a bug?



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Advertisement
paulcoz, like everyone has said, C/C++ will quit evaulating stuff the first chance it gets. For a series of and''s this means when you hit the first false. For a series of or''s it would be the first true.

This behavior is called "short-circuiting" and I think it''s in the language spec. Not all languages do this.

-Mike
-Mike

This topic is closed to new replies.

Advertisement