if() statement problem!
Its a wierd one, but the other day, and it happens quite regularly especially with structs and/or floats, if I use the following code technique, i.e. if I use "&&".
// Check if the Sprites'' XVel is slow.
if( Sprite[12].XVel > -0.3f && Sprite[12].XVel < 0.3f)
{
sprite_setaction(12,SLOW_WALK);
bla bla..
}
(Where Sprite[] is from a struct I created for each sprite, and XVel is a float variable holding the sprites'' horz velocity.)
It will never enter the code in the if()! Why?
I have to re-code it like this..
// Check if the Sprites'' XVel is slow.
if( Sprite[12].XVel > -0.3f)
{
if(Sprite[12].XVel < 0.3f)
{
sprite_setaction(12,SLOW_WALK);
bla bla..
}
}
Any ideas anyone? )-;
Downloads: ZeroOne Realm
I''m not immediately sure why it wouldn''t work the way you have it in the first example, but you could try putting ()''s around the individual tests, and seeing if that fixes the problem.
Example:
// Check if the Sprites'' XVel is slow.
if((Sprite[12].XVel > -0.3f) && (Sprite[12].XVel < 0.3f))
{
sprite_setaction(12,SLOW_WALK);
bla bla..
}
I must say that I''ve never had a problem with doing it like the first example shows, but I haven''t dealt all too much with the floating point end of it lately. More with regular integers. I know it wouldn''t be a problem with the structure/array usage, though, as I do it all the time. Comes with MUD programming.
All I can say is try it and see...
Example:
// Check if the Sprites'' XVel is slow.
if((Sprite[12].XVel > -0.3f) && (Sprite[12].XVel < 0.3f))
{
sprite_setaction(12,SLOW_WALK);
bla bla..
}
I must say that I''ve never had a problem with doing it like the first example shows, but I haven''t dealt all too much with the floating point end of it lately. More with regular integers. I know it wouldn''t be a problem with the structure/array usage, though, as I do it all the time. Comes with MUD programming.
All I can say is try it and see...
if( Sprite[12].XVel > -0.3f && Sprite[12].XVel < 0.3f)
means:
Sprite[12].XVel > -0.3f (first this is evaluated to true/false)
then that result is used with:
true/fase(1/0) && Sprite[12].XVel (making some useless value)
then that result is:
useless < 0.3f (and if this is true/false thats what the if acts on)
so you see without parenthesis c++ simply does what you tell and and evaluates it from left to right.
Edited by - Atavist on 2/28/00 1:33:11 AM
means:
Sprite[12].XVel > -0.3f (first this is evaluated to true/false)
then that result is used with:
true/fase(1/0) && Sprite[12].XVel (making some useless value)
then that result is:
useless < 0.3f (and if this is true/false thats what the if acts on)
so you see without parenthesis c++ simply does what you tell and and evaluates it from left to right.
Edited by - Atavist on 2/28/00 1:33:11 AM
Just because the church was wrong doesn't mean Galileo wasn't a heretic.It just means he was a heretic who was right.
You might be experiencing "short circuit evalutaion" which is always used on && and // from left to right.
The left handed expression will determine if it contiues or "short circuits" the rest of the evaluation.
For example:
The left handed expression will determine if it contiues or "short circuits" the rest of the evaluation.
For example:
#include "iostream"class A{public: bool foo();};bool A::foo(){ bool bRetVal = false; try { std::cout << std::endl << "This is foo being executed" << std::endl; bRetVal = true; } catch( ... ) { bRetVal = false; } return bRetVal;}int main( int argv, char **argc, char **env ){ A *pA = 0; // // if pA is non-zero then proceed with calling pA->foo() function // (pA) && (pA->foo()); pA = new A; // // if pA is not non-zero then proceed with calling pA-foo() function // (!pA) && (pA->foo()); delete pA; return 0;}
~deadlinegrunt
Atavist, most compilers do not just evaluate from left to right. Instead they use rules of precedence to determine which sections are evaluated first. In this example, the parentheses tell the compiler to do what it would do anyway. Unless, of course, you are using some odd compiler that doesn''t use precedence.
Wraith
AFT Software
Current Project: Hollow Point
Wraith
AFT Software
Current Project: Hollow Point
[email=cdickinson@scu.edu]Wraith[/email]BasketQase Software
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement