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 + 3is an expression.+is the operator.5and3are 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, andaandbare 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 = 910 - 4 = 65 * 3 = 1512 / 4 = 310 % 3 = 1
2. Relational Operators
- Used to compare two values.
- The result is usually boolean:
trueorfalse. - Examples:
5 > 3→true8 == 8→true4 != 4→false
3. Logical Operators
- Used to combine or negate conditions.
- Examples:
A AND BA OR BNOT A
- These are commonly used in decisions and loops.
4. Assignment Operators
- Used to store or update values in variables.
- Examples:
x = 10x += 5meansx = x + 5y *= 2meansy = 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
xor as complex as(a + b) * (c - d) / e.
Expressions are used in calculations, conditions, and assignments.
- They appear in mathematical formulas,
ifstatements, loops, and output statements. For example,marks >= 40is a relational expression, anda + b * cis 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 is14, not20.
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 = 83 + 8 = 11
With parentheses:
(3 + 4) * 2
Evaluation:
3 + 4 = 77 * 2 = 14
Working / Process
1. Identify the operands and operators
- Break the expression into values and symbols.
- Example: in
x = 5 + 3, operands arex,5, and3; 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) * 310 + 2 = 1212 * 3 = 36resultbecomes36
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