Operators and Expressions

Comprehensive study notes, diagrams, and exam preparation for Operators and Expressions.

Operators and Expressions

Definition

An operator is a symbol or keyword that tells the computer to perform a specific operation on one or more operands.

An expression is a valid combination of operands, operators, and sometimes function calls that evaluates to a single value.

Examples:

  • 5 + 3 is an expression.
  • + is the operator.
  • 5 and 3 are operands.
  • The result of the expression is 8.

Main Content

1. Operators

Operators are symbols or words used to perform operations on data.

  • They act on operands and produce a result. For example, in a + b, + is an arithmetic operator, and a and b are operands.

Different types of operators serve different purposes.

  • Common categories include arithmetic operators (+, -, *, /, %), relational operators (==, !=, >, <, >=, <=), logical operators (AND, OR, NOT), assignment operators (=, +=, -=, *=, /=), and bitwise operators (&, |, ^, <<, >>).

Types of Operators

1. Arithmetic Operators

  • Used for mathematical calculations.
  • Examples:
    • 7 + 2 = 9
    • 10 - 4 = 6
    • 5 * 3 = 15
    • 12 / 4 = 3
    • 10 % 3 = 1

2. Relational Operators

  • Used to compare two values.
  • The result is usually boolean: true or false.
  • Examples:
    • 5 > 3true
    • 8 == 8true
    • 4 != 4false

3. Logical Operators

  • Used to combine or negate conditions.
  • Examples:
    • A AND B
    • A OR B
    • NOT A
  • These are commonly used in decisions and loops.

4. Assignment Operators

  • Used to store or update values in variables.
  • Examples:
    • x = 10
    • x += 5 means x = x + 5
    • y *= 2 means y = y * 2

5. Unary Operators

  • Operate on a single operand.
  • Examples:
    • -x
    • ++x
    • !flag
  • They can change a value or its logical state.

2. Expressions

Expressions are combinations of operands and operators that evaluate to a value.

  • An expression can be as simple as a single variable like x or as complex as (a + b) * (c - d) / e.

Expressions are used in calculations, conditions, and assignments.

  • They appear in mathematical formulas, if statements, loops, and output statements. For example, marks >= 40 is a relational expression, and a + b * c is an arithmetic expression.

Types of Expressions

1. Arithmetic Expressions

  • Use arithmetic operators to perform calculations.
  • Example:
    • x = 10 + 5 * 2
  • Here, multiplication is performed before addition, so the result is 20.

2. Relational Expressions

  • Compare values and return a boolean result.
  • Example:
    • age >= 18
  • This is used in conditions like eligibility checks.

3. Logical Expressions

  • Combine relational expressions using logical operators.
  • Example:
    • (marks >= 40) AND (attendance >= 75)
  • This helps in decision-making.

4. Assignment Expressions

  • Assign the result of an expression to a variable.
  • Example:
    • sum = a + b

5. Mixed Expressions

  • Contain more than one type of operator.
  • Example:
    • (a + b) > (c - d) AND x != 0
  • These are common in real programs and require attention to precedence.

3. Operator Precedence and Associativity

Operator precedence determines the order in which operators are evaluated.

  • Higher-precedence operators are applied first. For example, multiplication happens before addition in 2 + 3 * 4, so the result is 14, not 20.

Associativity determines the direction of evaluation when operators have the same precedence.

  • For example, subtraction usually evaluates left to right: 10 - 4 - 2 = 4, because (10 - 4) - 2 = 4.

Order of Evaluation

Priority Operators Example
Highest Parentheses ( ) (2 + 3) * 4 = 20
High Multiplication, Division, Modulus 8 / 2 * 3 = 12
Medium Addition, Subtraction 5 + 2 - 1 = 6
Lower Relational 5 > 3
Lower Logical true AND false
Lowest Assignment x = 10 + 5

Why precedence matters

  • It avoids ambiguity in expressions.
  • It helps the computer decide the correct order of operations.
  • Parentheses can be used to override normal precedence and make expressions clearer.

Example

Expression:

3 + 4 * 2

Evaluation:

  • 4 * 2 = 8
  • 3 + 8 = 11

With parentheses:

(3 + 4) * 2

Evaluation:

  • 3 + 4 = 7
  • 7 * 2 = 14

Working / Process

1. Identify the operands and operators

  • Break the expression into values and symbols.
  • Example: in x = 5 + 3, operands are x, 5, and 3; operators are = and +.

2. Apply precedence and associativity

  • Determine which operation happens first.
  • Parentheses first, then multiplication/division, then addition/subtraction, and so on.

3. Evaluate the expression and store or use the result

  • Compute the final value.
  • If assignment is present, save the result in a variable.
  • Example:
    • result = (10 + 2) * 3
    • 10 + 2 = 12
    • 12 * 3 = 36
    • result becomes 36

Advantages / Applications

Helps perform calculations efficiently

  • Operators make arithmetic and data processing simple and fast.
  • Used in accounting, engineering, science, and everyday software tasks.

Supports decision-making in programs

  • Relational and logical expressions help programs choose actions.
  • Used in login checks, grading systems, eligibility tests, and validation logic.

Makes code concise and readable

  • Operators allow complex operations to be written in short forms.
  • Expressions help combine multiple steps into a clear statement.

Summary

  • Operators are symbols used to perform operations, and expressions are combinations that produce a value.
  • Different operators handle calculation, comparison, logic, and assignment.
  • Expression evaluation depends on precedence and associativity.
  • Terms to remember: operator, operand, expression, precedence, associativity