IF( ) Statement

Notes:
1.  Use "= =" for IF statements.  If you use "=", the variable on the left will be assigned the value on the right and the IF result will depend on the resulting value (See note 2).
2.  An variable is FALSE if its value is zero and TRUE if it is not zero. (Any value other than zero will equate to TRUE).
3.  The variables "TRUE" and "FALSE" are usually defined in PICC as 0x01 and 0x00 respectively (In the processor header file) but generally not defined in most other compilers.  When comparing a variable to these constants, care must be taken if the variable is more than 1 bit wide.
4.  Most compilers will not allow single bit variables other than those related to actual I/O.  A single bit, or BOOLEAN, variable is typically the LSB of an 8 bit RAM location with the other 7 bits wasted.  This is a result of ANSI compliance not taking advantage of the fact that bit operations work for user RAM just as well as I/O (Which is usually mapped into the RAM space).  Full utilization of all 8 bits can be achieved in ANSI compilers, but at the expense of bit masked operations.  PICC stacks BOOLEAN variables into bytes, and uses the fast bit wise instructions to test them.
CHAR tempxx;
INT1
flag_bit;

///////////////////////////////////
//these are all equivalent (1 Bit)

IF (flag_bit)      
    tempxx=100;

IF (flag_bit==1)
    tempxx=100;

IF (flag_bit==1)
    {
    tempxx=100;
    }

IF (flag_bit==TRUE)
    tempxx=100;
///////////////////////////////////
//these are not equivalent (>1 Bit)

IF (tempxx)        //this would execute if
    tempxx=50;     //tempxx were 0x01 to 0xFF

IF (tempxx==TRUE)  //true is 0x01 only
    tempxx=50;
///////////////////////////////////
//this is valid
IF (flag_bit)
    IF (tempxx=50)
        tempxx=0;
///////////////////////////////////
//this will compile ok but generate
//no actual code because the bit
//address 0f 0x40 would never be 0:
//tempxx would never be set to 100
//here!
IF (!PIN_A0) //Use (!INPUT(PIN_A0))
    {
    tempxx=100;
    }
///////////////////////////////////
//By the same logic of the previous
//example, the bit address 0f 0x40
//would always be true:  tempxx
//would always be set to 100 here!
IF (PIN_A0) //Use (INPUT(PIN_A0))
    {
    tempxx=100;
    }
IF ((bit1 = 1) AND (bit2 = 1)) IF (bit1 && bit2)
IF ((bit1 = 0) AND (bit2 = 0)) IF (!bit1 && !bit2)
IF (PIN_A0 = 1 AND PIN_B0 = 1) IF (INPUT(PIN_A0) && INPUT(PIN_B0))
IF ((bit1 AND bit2)=0) IF (!(bit1 & bit2))

Any questions or comments?
 This page last updated on August 28, 2011