 # Common Pitfalls in GCL+ Programming (Part 2 – Using And, Or)

In a recent post, we introduced boolean logic and the AND, OR, and NOT operators.  Now we will show some common ways in which these can be accidentally misused in a GCL+ program, and how to correct these errors.

AND

As we saw previously, the AND operator compares two boolean (true or false) statements.  If and only if BOTH statements are true, the entire statement will be true.  This is very commonly used in GCL+ programs.  There are some important things to watch out for, however!

For example, this bit of programming looks just fine, but actually won’t work the way you would expect:

If AHU1_LOW_LIMIT And AHU1_SMOKE = Normal Then

// Commands we want to execute would be here

End If

Here’s what’s happening.  When you use “And”, GCL+ will evaluate what’s on either side and compare the two things.  If they’re both true, the whole condition will be true.  When it sees AHU1_LOW_LIMIT all by itself, it evaluates it.  In this case, “Normal” is OFF and “Alarm” is ON. In GCL+, ON is the same as true and OFF is the same as false.  So the equivalent is:

If AHU1_LOW_LIMIT = On And AHU1_SMOKE = Normal Then

// Commands we want to execute would be here

End If

This is the same as:

If AHU1_LOW_LIMIT = Alarm And AHU1_SMOKE = Normal Then

// Commands we want to execute would be here

End If

As you can see, this is not the intended result.  Here is the correct way to do it:

If AHU1_LOW_LIMIT = Normal And AHU1_SMOKE = Normal Then

// Commands we want to execute would be here

End If

OR

The same thing applies if you’re using “Or”.  The OR operator compares two boolean statements.  If either of the two statements is true, the overall statement is true.  This is the correct way to do it:

If AHU1_LOW_LIMIT = Alarm Or AHU1_SMOKE = Alarm Then

// Commands we want to execute would be here

End If