Operator precedence tells the computer which part of a math or logic expression to do first. Evaluation order is the sequence in which the computer calculates each part.
Operator precedence and evaluation order in C Sharp (C#)
expression = operand operator operand // Example: int result = 3 + 4 * 2;
Operators like * and / have higher precedence than + and -.
Parentheses () can change the order by forcing parts to be evaluated first.
int result = 3 + 4 * 2; // result is 11
int result = (3 + 4) * 2; // result is 14
bool check = true || false && false; // check is true
bool check = (true || false) && false; // check is false
This program shows how operator precedence and parentheses affect the results of math and logical expressions.
using System; class Program { static void Main() { int a = 5 + 3 * 2; int b = (5 + 3) * 2; bool c = true || false && false; bool d = (true || false) && false; Console.WriteLine($"a = {a}"); Console.WriteLine($"b = {b}"); Console.WriteLine($"c = {c}"); Console.WriteLine($"d = {d}"); } }
Remember that operators with higher precedence run before those with lower precedence.
Use parentheses to make your code easier to read and to control evaluation order.
Evaluation order matters when expressions have side effects like method calls.
Operator precedence decides which operations happen first in an expression.
Parentheses can change the normal precedence to control evaluation order.
Understanding this helps avoid bugs and write clearer code.
Practice
3 + 4 * 2?Solution
Step 1: Recall operator precedence rules
Multiplication (*) has higher precedence than addition (+) in C#.Step 2: Apply precedence to the expression
The multiplication 4 * 2 is evaluated first, then 3 is added.Final Answer:
Multiplication (*) -> Option DQuick Check:
Multiplication before addition = Multiplication (*) [OK]
- Thinking addition happens before multiplication
- Ignoring operator precedence
- Assuming left-to-right always applies
Solution
Step 1: Check parentheses balance and syntax
int result = 3 + (4 * 2); has balanced parentheses around (4 * 2), which is correct syntax.Step 2: Verify other options for syntax errors
Options A and C have unbalanced parentheses, and D has no parentheses to change order.Final Answer:
int result = 3 + (4 * 2); -> Option CQuick Check:
Balanced parentheses change order correctly [OK]
- Leaving parentheses unbalanced
- Placing parentheses incorrectly
- Forgetting to close parentheses
int a = 5; int b = 3; int c = 2; int result = a - b + c * 4; Console.WriteLine(result);
Solution
Step 1: Identify operator precedence in the expression
Multiplication (*) has higher precedence than addition (+) and subtraction (-), so c * 4 is evaluated first: 2 * 4 = 8.Step 2: Evaluate the remaining expression left to right
Now compute a - b + 8: 5 - 3 = 2, then 2 + 8 = 10.Final Answer:
10 -> Option AQuick Check:
Multiplication before addition/subtraction [OK]
- Adding before multiplying
- Ignoring operator precedence
- Calculating left to right without precedence
int x = 10; int y = 5; int z = x / y * 2 +; Console.WriteLine(z);
Solution
Step 1: Check the expression for syntax errors
The expression ends with a '+' operator without a following operand, which is invalid syntax.Step 2: Verify other parts of the code
Variables x and y are declared correctly, division operator is valid, and Console.WriteLine syntax is correct.Final Answer:
Missing operand after '+' operator -> Option BQuick Check:
Operator must have operands on both sides [OK]
- Ignoring incomplete expressions
- Assuming division operator is wrong
- Overlooking missing semicolons
result? Consider operator precedence and evaluation order.int a = 2; int b = 3; int c = 4; int result = a + b * c - (a + b) * c / b;
Solution
Step 1: Evaluate expressions inside parentheses first
(a + b) = 2 + 3 = 5.Step 2: Calculate multiplication and division next
b * c = 3 * 4 = 12.
(a + b) * c / b = 5 * 4 / 3 = 20 / 3 = 6 (integer division truncates).Step 3: Calculate the full expression left to right
a + b * c - (a + b) * c / b = 2 + 12 - 6 = 14 - 6 = 8.Step 4: Re-check integer division
20 / 3 = 6 (integer division truncates decimals).Final Answer:
8 -> Option AQuick Check:
Parentheses first, then multiply/divide, then add/subtract [OK]
- Ignoring integer division truncation
- Evaluating left to right without precedence
- Calculating parentheses last
Ready to prove it?
Pass the test and this pattern moves unknown
