Understand and use arithmetic, logical and Boolean operators

Resources | Subject Notes | Computer Science

Arithmetic, Logical and Boolean Operators

Introduction

In programming, operators are symbols that perform operations on variables and values. This section focuses on arithmetic, logical, and Boolean operators, which are fundamental for decision-making and calculations within programs. Understanding how to use these operators effectively is crucial for writing efficient and correct code.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. The most common arithmetic operators are:

  • Addition: $+$
  • Subtraction: $-$$
  • Multiplication: $\times$ or $*$
  • Division: /$ or $/$
  • Modulus: $\%$ (returns the remainder of a division)

Examples

Here are some examples of how arithmetic operators are used:

  1. Calculate the sum of two numbers: $result = a + b$
  2. Find the difference between two numbers: $difference = a - b$
  3. Calculate the product of two numbers: $product = a \times b$
  4. Calculate the quotient of two numbers: $quotient = a / b$
  5. Find the remainder when a number is divided by another: $remainder = a \% b$
Operator Description Example Result
+ Addition $5 + 3$ $8$
- Subtraction $10 - 4$ $6$
$\times$ Multiplication $6 \times 7$ $42$
$/$ Division $15 / 3$ $5$
$\%$ Modulus (remainder) $17 \% 5$ $2$

Logical Operators

Logical operators are used to combine or modify boolean expressions (which evaluate to either true or false). The main logical operators are:

  • AND: $\land$ (or \&) - Returns true if both operands are true.
  • OR: $\lor$ (or |) - Returns true if at least one operand is true.
  • NOT: $eg$ (or !) - Reverses the truth value of an operand.

Truth Tables

Here are the truth tables for the logical operators:

AND

Operand 1 Operand 2 Result
True True True
True False False
False True False
False False False

OR

Operand 1 Operand 2 Result
True True True
True False True
False True True
False False False

NOT

Operand Result
True False
False True

Boolean Operators

Boolean operators are used to evaluate boolean expressions. They are essentially the logical operators discussed above. They are used to create complex conditions that can be evaluated as either true or false.

Examples

Here are some examples of how logical operators are used in programming:

  1. Check if a number is within a range: $a > 10 \land a < 20$
  2. Check if a user is logged in or has admin privileges: $(userLoggedIn \lor isAdmin)$
  3. Reverse a boolean condition: $eg (isError)$

Operator Precedence

Operator precedence determines the order in which operations are performed in an expression. Generally, the order is as follows (from highest to lowest precedence):

  1. Parentheses ()
  2. NOT ( )
  3. Multiplication ($\times$, $\%$ ) and Division ($/$, $/$ )
  4. Addition ($+$, $-$)
  5. AND ($\land$)
  6. OR ($\lor$)

Using parentheses can be used to override the default precedence and ensure that expressions are evaluated as intended.