Author Topic: Testing a Variable for True/False  (Read 377 times)

Legacy_BelowTheBelt

  • Hero Member
  • *****
  • Posts: 699
  • Karma: +0/-0
Testing a Variable for True/False
« on: October 17, 2013, 07:38:29 pm »


               I had a simple question about testing for TRUE/FALSE using variables.

So the code:
if(GetLocalInt(oPC)) is "true" if it returns any number and "false" if it returns 0.

if I assign a value to a variable in the code
int iVar = 47;
or
int iVar = TRUE;

Does the test:
if (iVar)
still return 'true'?

And does !!iVar test to see if iVar is 0/FALSE? 

My sense is that it does, but wanted to clarify.

Thanks!
 
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Testing a Variable for True/False
« Reply #1 on: October 17, 2013, 07:56:22 pm »


               NWN script and most High Level Languages do not check if something is TRUE in the compiled code.    0 (FASLE) is the only thing checked for.    therefore   0 is FALSE and everything else is TRUE ( !=0 )
               
               

               
            

Legacy_BelowTheBelt

  • Hero Member
  • *****
  • Posts: 699
  • Karma: +0/-0
Testing a Variable for True/False
« Reply #2 on: October 17, 2013, 08:51:59 pm »


               Ok, thanks for the clarification - it's testing for false or not false.

But to get to the question in the example above, i can declare a variable with a value and use the if statement to test for false/not false using the variable name, correct?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Testing a Variable for True/False
« Reply #3 on: October 17, 2013, 09:05:44 pm »


               Correct,  


If ( Expression )  

The Expression in the  if Statement can be anything that evaluates to a single integer.     So any  integer,  function that returns an integer, comparison , or mat problem will do.
               
               

               
            

Legacy_BelowTheBelt

  • Hero Member
  • *****
  • Posts: 699
  • Karma: +0/-0
Testing a Variable for True/False
« Reply #4 on: October 17, 2013, 09:27:57 pm »


               Excellent!  Thanks for that.  That's what I thought, though your explanation is more comprehensive than my understanding.

I see a lot of code that tests if (iVar == TRUE) in some of the code that I'm using (habd or the AI, for example) when it seems like if (iVar) would be sufficient and more efficient - especially in systems that are utilized frequently in the module.  

While the efficiency gained might be minimal by removing any single instance of this in a module, it also seems that some efficiency can be gained by removing the unnecessary comparison module-wide.
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Testing a Variable for True/False
« Reply #5 on: October 18, 2013, 12:58:52 am »


               It's mainly done like that for clarity and maintainability of the code. Personally I would use if(var) as it is clear enough for me.

TR