Simple comparision question

Posted By: Anonymous

Simple comparision question - 06/18/13 16:52

The manual says you should not do a Boolean check on a pointer and comp it's member(s) values in the same if(). Is this still true. Example below.

Code:
if ( pan_temp && pan_temp.pos_x != 1024 ) // The manual says don't do this.

if ( pan_temp )  // Manual say do this.
{
   if ( pan_temp.pos_x != 1024 );
}



Also does this apply if you are doing the Boolean check on the contents of a VAR and not it's pointer.

Code:
var I = 999;

if ( I && I > 500 )

Posted By: 3run

Re: Simple comparision question - 06/18/13 17:15

Why do you need to check 'var' for? If it's not declared, it won't compile anyway, or am I getting your point wrong?

Greets
Posted By: rayp

Re: Simple comparision question - 06/18/13 17:25

Quote:

The manual says you should not do a Boolean check on a pointer and comp it's member(s) values in the same if(). Is this still true. Example below.


Still true. Maybe in near future it will be changed.
Posted By: Anonymous

Re: Simple comparision question - 06/18/13 17:32

@3Run I am checking if the vars content is != 0. Content not pointer. I am trying to see if it has a value not if it was declared.

Code:
if ( I ) is same as if(I != 0)



But I might to totally wrong in my understand of what I did above.


@rayp Thank you for this reply. That's is where my code is breaking.
Posted By: rayp

Re: Simple comparision question - 06/18/13 17:34

Code:
if (!i) _empty();
if (i) _notempty();

Do u mean this ?

edit: So if (i) should be the same as if (i!=0) yes.
Posted By: Anonymous

Re: Simple comparision question - 06/18/13 17:36

@rayp Yes that is what I'm asking. Does the same rule apply as pointers here

Code:
if( i && i != 500) // Bad ?

if(i)   // Good?
{ 
   if(i != 500)
}



I have to stop using i because of autocaps for i
Posted By: rayp

Re: Simple comparision question - 06/18/13 17:43

no you dont need to check VAR's before.

edit:
Code:
var test = 1;
if (test > 0 && test < 2) _test_is_one();
if (!test) _test_is_notone();

ENTITY* test2;
if (test2) if (test2.x == 10) ...

Posted By: Kartoffel

Re: Simple comparision question - 06/18/13 17:48

The manual says that this is "good" because the program checks the whole comparision before deciding if it is true or false.

Code:
ENTITY * ent;

void main()
{
	if(ent && ent.x > 5)
		ent.x = 0;
}



this will result in an error.

ent doesn't exist so if you check if(ent) it is true.
but in the comparison above the program doesn't abort it but checks the next comparison (ent.x > 5)
but ent doesn't exist and this causes an error.

edit: note that this only applies for pointers.
Posted By: 3run

Re: Simple comparision question - 06/18/13 17:49

Why don't you use something like this:
Code:
var checkVar(var CHECK){
	if(CHECK != 0){ return(1); }
	if(CHECK == 0){ return(0); }
}

Hope, it helps grin

Greets
Posted By: krial057

Re: Simple comparision question - 06/18/13 17:56

EDIT: sorry, only read your first post when i wrote it ^^
Quote:
The manual says you should not do a Boolean check on a pointer and comp it's member(s) values in the same if(). Is this still true.


Yes.

Quote:
Also does this apply if you are doing the Boolean check on the contents of a VAR and not it's pointer.


It depends of what you want to do. variables in if's evaluate to "false" if they are 0 and to "true" if they are non-zero. Here are some examples:
Code:
i=10;
if(i) -> if(true) -> will execute
i = -20;
if(i) -> if(true) -> will execute
i = 0;
if(i) -> if(false) -> will not execute



so your second example will work only if I > 500 ("I &&" is useless, because if(i) means if i is not 0. But your second contion I>500 includes that i is not null).

Why do you need to check it with poitners in an if?
3dgs evaluates all conditions in the if. If you have something like the following:
Code:
pan_temp = NULL;
if( pan_temp && pan_temp.pos_x != 1024 )


It will evaluate to the following:
Code:
if(NULL && NULL.pos_x != 1024 )


And because NULL has no property pos_x, the compiler will show an error.
Posted By: Anonymous

Re: Simple comparision question - 06/18/13 17:57

@rayp Thank you - dang my logic yes no need to do a content check sometimes I'm just so slow eek

@Kartoffel That makes things clearer I just didn't really understand it in the manual. Slow again lol.

@3Run That is useful but like rayp pointed out my logic is pointless.

@Krial057 Thank you to.. I see your points as well. Yes now if only I could fix the person writing the code. lol


Thank you all very much for your time.
Mal
© 2024 lite-C Forums