Author Topic: Determining TRUE/FALSE  (Read 281 times)

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Determining TRUE/FALSE
« on: November 27, 2011, 03:41:16 pm »


                So... while the constants
TRUE = 1
and
FALSE = 0

When you evaluate a statement, it seems to me that evaluating a statement as true is different.
Its pretty clear to me that the following always evaluates as true

int x=8;

if(x)
     return TRUE;
else
    return FALSE;

but what about checking for false?

The reason I ask is that some functions return -1 on error instead of 0. And I was wondering if all values less than 1 would evaluate as FALSE.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #1 on: November 27, 2011, 03:55:43 pm »


               if(x) is true as long as the value is non zero, if you are dealing with functions that can return only 0 and 1, you dont have to use == TRUE equalation, if the values can be different, then you have to use appropriate check like != 0, > -1, < 1 etc
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #2 on: November 27, 2011, 04:02:19 pm »


               Damn. I thought that was the case. Thanks.
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #3 on: November 27, 2011, 04:03:57 pm »


               <peering over...>

Out of curiosity, what functions are returning -1 as FALSE?

<...his peepers>
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #4 on: November 27, 2011, 05:01:48 pm »


               

Rolo Kipp wrote...

<peering over...>

Out of curiosity, what functions are returning -1 as FALSE?

<...his peepers>

The question doesn't have a sense '<img'>. Or you mean when the -1 should be FALSE?

The default "boolean" functions returns only TRUE or FALSE. If you are making your own function like GetHasSubString, then once you get -1 from FindSubString function, you should return FALSE in your function definition.

If you choose to return the position, that is what FindSubString returns, then you have to check if the function returns value greater than -1 to see if string contain substring or not.
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #5 on: November 27, 2011, 05:12:25 pm »


               <huffs and puffs...>

ShaDoOoW wrote...
The question doesn't have a sense '<img'>. Or you mean when the -1 should be FALSE?

Of *course* it has a sense! Or is that tense?

Anyway, I mean a function that *pretends* to return FALSE, but sneakily and with malice aforethought returns -1. Surely that's clear to anyone who is fluent in West Coast Colloquial American. Hmph!

The default "boolean" functions returns only TRUE or FALSE. If you are making your own function like GetHasSubString, then once you get -1 from FindSubString function, you should return FALSE in your function definition.

If you choose to return the position, that is what FindSubString returns, then you have to check if the function returns value greater than -1 to see if string contain substring or not.

Yeah, *that's* my real question... Whether H was finding Bioware functions that should be returning boolean integer, but are behaving badly and returning -1.

<...and pretends to be offended>
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #6 on: November 27, 2011, 05:43:32 pm »


               

Rolo Kipp wrote...

Anyway, I mean a function that *pretends* to return FALSE, but sneakily and with malice aforethought returns -1. Surely that's clear to anyone who is fluent in West Coast Colloquial American. Hmph!


The GetSubSrting Function never stated that it returnsTrue or   False.   It states that it returns the position of the substring.   position zero is the first chatacter in the string.   It returns -1 On_Error Or rather No SubString Found.   
               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #7 on: November 27, 2011, 06:03:02 pm »


               Well... if you are getting an integer value that represents an index in a 2DA it becomes problematic to know if the value has been set or not, because in that case 0 is a valid index.

To get around this I have initialized these values as -1 to mean no index.

But unfortunately I don't believe -1 will read as FALSE when evaluating an expression, and that was my problem. In essence I need to run a check for NOINDEX and set const int NOINDEX = -1.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #8 on: November 27, 2011, 06:22:04 pm »


               

henesua wrote...

 So... while the constants
TRUE = 1
and
FALSE = 0

When you evaluate a statement, it seems to me that evaluating a statement as true is different.
Its pretty clear to me that the following always evaluates as true

int x=8;

if(x)
     return TRUE;
else
    return FALSE;

but what about checking for false?

The reason I ask is that some functions return -1 on error instead of 0. And I was wondering if all values less than 1 would evaluate as FALSE.


I am going to expand on what Shadow already stated.  
First lets look at the comparison operators and expand on them a lillte.   
They are in fact operators and as operators the can be used any where that operators can be used at.  

As an Example lets when giving 500 xp to a character i wanted to give then an extra 50xp  if they where first level. with the "Bonus".  I would not even have to use an If statment, Since the '==' is an operator that returns  1 or 0. (TRUE or FALSE).  I could evalulate it like this. 

int nXP = 500 + (GetHitDice(oPC)== 1)* 50;

If the PC has only one level  then GetHitDice(oPC)== 1 evalulates to 1 and the 50 gets added.  // 1*50 = 50;
If they are not first level then the GetHitDice(oPC)== 1 evelaulates to 0 and the 50 does not get added. // 0 * 50 = 0.
 
Note: the Above is leagle to do it just breaks convention

**********

Ok the If statment:

If (Expresion) ...

The Expresion is anything that evalulates to an intenger.  
Now  the Expresion is never checked to see if it is TRUE or not.    It is only checked for if it is FALSE.    If it is FALSE(0)  the execution order Jumps over the code placed in the TRUE bracket.  If it is not false then no jump is taken and the code excution falls through to the TRUE case.     

In short the only thing that matters is if you expression is Zero or Not Zero. 

If ( Expression) 
{
    // Code excuted if the Expression !=0
}
else 
{
     // Code excuted if the Expression == 0
}   

*****

with all that said  the if statment you posted above. 
int x=8;

if(x)
     return TRUE;
else
    return FALSE;

would be the same thing as.

return x != 0;  // Returns FALSE if x is zerp, Returns TRUE for everything else;  


Hope that helps.
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
Determining TRUE/FALSE
« Reply #9 on: November 27, 2011, 06:27:19 pm »


               <getting a...>

Now, *that's* a sweet little explanation :-)

<...toothache>