Expression Evaluation
Expression evaluation is the process of computing the final value of an expression by following operator precedence, associativity, and parentheses.
1) What is an expression?
An expression is made of:
- Operands: constants, variables, or function-call results
- Operators: symbols such as
+,*,%,&&,|,? :
Example: a + b * c
2) Operator categories (quick recap)
- Arithmetic:
+,-,*,/,% - Relational:
>,<,>=,<=,==,!= - Logical:
&&,||,! - Bitwise:
&,|,^,~,<<,>> - Conditional (ternary):
condition ? expr1 : expr2 - Assignment:
=,+=,-=,*=,/=,%=,&=,^=,|=,<<=,>>=
3) Precedence and associativity
- Precedence decides which operator is evaluated first.
- Associativity decides direction when operators are on the same precedence level.
- Parentheses always have priority and can override default order.
Example
a + b * c->b * cfirst (higher precedence of*)a - b - c->(a - b) - c(left to right)a = b = c->a = (b = c)(right to left)
4) Operator precedence table
| Level | Operators | Description | Associativity |
|---|---|---|---|
| 1 | [], ., (), ++, -- | Postfix/member/call | Left to Right |
| 2 | ++, --, +, -, !, ~ | Unary operators | Right to Left |
| 3 | () (cast), new | Cast/object creation | Right to Left |
| 4 | *, /, % | Multiplicative | Left to Right |
| 5 | +, - | Additive | Left to Right |
| 6 | <<, >> | Shift | Left to Right |
| 7 | <, <=, >, >= | Relational | Left to Right |
| 8 | ==, != | Equality | Left to Right |
| 9 | & | Bitwise AND | Left to Right |
| 10 | ^ | Bitwise XOR | Left to Right |
| 11 | | | Bitwise OR | Left to Right |
| 12 | && | Logical AND | Left to Right |
| 13 | || | Logical OR | Left to Right |
| 14 | ?: | Conditional | Right to Left |
| 15 | =, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= | Assignment | Right to Left |
5) Common mistakes to avoid
Ignoring precedence
- Writing
2 + 3 * 4and assuming(2 + 3) * 4. - Correct order:
3 * 4first, then2 + 12.
- Writing
Confusing associativity with precedence
- In
a - b - c, some learners trya - (b - c). - Correct due to left associativity:
(a - b) - c.
- In
Missing parentheses in mixed logical/relational expressions
- Expression like
a > b && c == d || e < fcan be hard to read. - Even if rules are known, add parentheses for clarity:
(a > b && c == d) || (e < f).
- Expression like
6) Quick self-check
Try these mentally before moving ahead:
8 - 3 - 2 = ?2 + 3 * 5 = ?(2 + 3) * 5 = ?7 > 3 && 2 == 2 = ?(use1for true,0for false)
Answers:
317251
7) Step-by-step solved example
Consider:
int result = 2 + 3 * 4 > 10 && 5 < 8 || 7 == 6;
Evaluation:
3 * 4 = 122 + 12 = 1414 > 10->1(true)5 < 8->1(true)1 && 1->17 == 6->0(false)1 || 0->1
So, result = 1.
8) Note on logical results (0 and 1)
In C-style evaluation:
trueis represented as1falseis represented as0
So relational and logical parts of expressions typically reduce to 0 or 1, which can then participate in further operations.
9) Practical checklist
When evaluating any expression:
- Resolve parentheses first.
- Apply precedence levels from high to low.
- For same-level operators, apply associativity.
- Reduce sub-expressions one step at a time.