Operators & Expressions
Software Applications & Programming – Cyber Olympiad (NCO)
“An operator is a symbol that tells the compiler to perform a specific operation. An expression is a combination of operators, operands, and values that produces a result.”
1. Simple Explanation
| Concept | What It Is | Why It Matters |
|---|---|---|
| Operator | A symbol or keyword that performs a specific action on one or more operands (e.g., +, &&, =). |
Enables manipulation of data and control of program flow. |
| Expression | A syntactically correct combination of operators and operands that yields a value (e.g., a + b * c). |
The building blocks of statements; they are evaluated to produce results. |
| Operand | The data that operators act upon (variables, literals, or sub‑expressions). | Holds the values being processed. |
2. Key Points
| Category | Highlights | Typical Syntax (C‑style) |
|---|---|---|
| Arithmetic Operators | + - * / % |
int sum = a + b; |
| Relational Operators | < <= > >= == != |
bool isEqual = (x == y); |
| Logical Operators | &&, ` |
|
| Bitwise Operators | `& | ^ ~ << >>` |
| Assignment Operators | `= += -= *= /= %= &= | = ^= <<= >>=` |
| Increment/Decrement | ++, -- (prefix/postfix) |
++i; |
| Conditional (Ternary) Operator | ?: |
int max = (a > b) ? a : b; |
| Comma Operator | , (evaluates left to right, returns rightmost) |
int x = (i = 0, i += 5); |
| Operator Precedence | Determines evaluation order (e.g., * before +). |
See table below. |
| Associativity | Left‑to‑right for most; right‑to‑left for = and ?:. |
Affects same‑precedence grouping. |
| Short‑Circuit | Logical && and ` |
|
| Type Promotion | Operands of different types are promoted to a common type. | int + double → double. |
| Side‑Effects | Some operators change state (e.g., ++, --, =). |
Must be used carefully in expressions. |
Operator Precedence (Highest → Lowest)
| Priority | Operators (C‑style) | Notes |
|---|---|---|
| 1 | ! ~ ++ -- (type) * & |
Unary |
| 2 | * / % |
|
| 3 | + - |
|
| 4 | << >> |
|
| 5 | < <= > >= |
|
| 6 | == != |
|
| 7 | & |
|
| 8 | ^ |
|
| 9 | ` | ` |
| 10 | && |
|
| 11 | ` | |
| 12 | ?: |
Ternary |
| 13 | = += -= etc. |
Assignment |
| 14 | , |
Comma |
3. Worked Example
Problem
Evaluate the expression and determine the final values of the variables.
int a = 5, b = 2, c = 7;
int result = a++ * b + --c % 3;
Step‑by‑Step Evaluation
| Step | Expression | Explanation | Value |
|---|---|---|---|
| 1 | a++ |
Post‑increment: value 5, then a becomes 6. |
5 |
| 2 | --c |
Pre‑decrement: c becomes 6, value 6. |
6 |
| 3 | --c % 3 |
6 % 3 = 0 |
0 |
| 4 | a++ * b |
5 * 2 = 10 |
10 |
| 5 | 10 + 0 |
10 |
10 |
| 6 | Assign to result |
result = 10 |
10 |
Final Variable Values
| Variable | Value |
|---|---|
a |
6 (post‑increment) |
b |
2 |
c |
6 (pre‑decrement) |
result |
10 |
Key Take‑aways
- Post‑increment uses the original value before changing the variable.
- Pre‑decrement changes the variable before it is used.
- Operator precedence dictates that
%is evaluated before+. - Assignment is the last operation performed.
4. Quick Revision Summary
- Operators perform actions; expressions combine them to produce a value.
- Arithmetic:
+ - * / %(integer division truncates). - Relational:
< <= > >= == !=evaluate tobool. - Logical:
&&,||,!– short‑circuit evaluation. - Bitwise:
& | ^ ~ << >>operate on binary representation. - Assignment:
= += -= *= /= %= &= |= ^= <<= >>=. - Increment/Decrement:
++,--(prefix vs postfix). - Ternary:
condition ? expr1 : expr2. - Precedence: Multiplication/division before addition/subtraction; relational before logical; assignment lowest.
- Associativity: Left‑to‑right for most; right‑to‑left for assignments and ternary.