Lesson Summary

Logical operators build on what we have previously learned, helping us to bring a greater degree of flexibility to our expressions. Provided with Boolean operands, logical operators return a resultant Boolean state (True or False) dependant on the value of those operands and the purpose of the operator itself:

  • And (&): Takes two operands. If both operands evaluate to True, the logical statement also equates to True.
  • Or (|): Takes two operands. If either operand evaluates to True, the logical statement also equates to True.
  • Not (!): Takes one operand. Whatever the operand evaluates to, adding the ! operator Negates that value – True becomes False and vice versa.

These operators allow us to better drive the flow of our expressions by combining multiple conditions. This helps us to keep our code as efficient as possible.

You should now be confident in the usage of the three primary logical operators, and understand how the operator precedence dictates the evaluation of expressions containing logical, arithmetic and comparison operators.

 

<< Previous: Logical Operator Precedence | Next: Test Your Knowledge! >>