Logical Operators
Logical (Boolean) operators are used to combine or invert conditions that evaluate to TRUE or FALSE. They’re essential for writing expressive, multi-condition IF statements.
Boolean Operations
Operator | Description | Example |
---|---|---|
AND | True if both are true | A AND B |
OR | True if either is true | A OR B |
NOT | Inverts a Boolean value | NOT A |
Operator Use in IF Statements
Logical operators are typically used inside IF conditions to control flow based on multiple inputs or states.
Example 1: Basic AND condition
IF Start_Button AND NOT Stop_Button THEN
Motor_Run := 1;
END_IF;
This will start the motor only if the start button is pressed and the stop button is not pressed.
Example 2: Using OR
IF Emergency_Stop OR Fault THEN
System_Enabled := 0;
END_IF;
This disables the system if either the emergency stop or a fault is active.
Example 3: Multiple combined conditions
IF (Level < 50) AND (Pump_Enabled OR Manual_Override) THEN
Pump := 1;
END_IF;
This turns on the pump if the level is low and either the pump is enabled or overridden manually.
Notes
-
Parentheses can be used to group logic for clarity and precedence.
-
Avoid long, complex IF conditions without parentheses — they can become hard to read and debug.
Best Practices
-
Keep Boolean expressions readable and explicit.
-
Use descriptive variable names so conditions make sense on their own.
-
Use NOT carefully — it only applies to the next expression, so grouping with parentheses is often safer.