C# Programming · Lesson 4
Smooth Operators
Evaluate C# arithmetic, comparisons, Boolean logic, conversions, precedence, and operator behavior.
Lesson purpose
Lesson 4 explains how operators turn typed values into results and decisions. Students learn arithmetic, remainder, precedence, assignment, increment, comparison, floating-point tolerance, Boolean logic, short-circuit evaluation, expression typing, conversions, and operator overloading.
Learning objectives
- Evaluate arithmetic expressions and explain integer division and modulo.
- Use parentheses, assignment, compound assignment, increment, and decrement deliberately.
- Build Boolean conditions with comparison and logical operators.
- Compare floating-point calculations with a tolerance.
- Explain how operand types determine an expression’s result type.
- Distinguish implicit promotion from an explicit cast and describe operator overloading.
1. Arithmetic operators
The arithmetic set includes unary negative, multiplication, division, addition, subtraction, and modulo:
int a = 17;
int b = 5;
int sum = a + b;
int difference = a - b;
int product = a * b;
int quotient = a / b;
int remainder = a % b;
Modulo is the remainder after integer division. It is useful for even/odd tests and repeating cycles. Integer division discards the fractional part, so make at least one operand a double or decimal when a fractional result matters.
2. Precedence, parentheses, and assignment
C# evaluates multiplication and division before addition and subtraction, with equal-precedence operations generally proceeding left to right:
int result1 = 2 + 3 * 4; // 14
int result2 = (2 + 3) * 4; // 20
int result3 = 20 / 5 * 2; // 8
Parentheses make intent visible:
int result = (7 % 3) * (4 + (6 / 3));
Assignment evaluates the right side and stores the result. Compound assignments combine an operation with assignment:
int count = 5;
count += 2;
count *= 3;
The target type must accept the expression result. = assigns; == compares.
3. Increment and decrement
count++ and ++count both increase count by one as standalone statements. Their difference appears when the expression’s value is used:
int n = 1;
int before = n++;
n = 1;
int after = ++n;
Use standalone increment when the purpose is simply to change the value. This keeps evaluation order visible.
4. Comparisons and floating-point tolerance
The comparison operators are ==, !=, <, <=, >, and >=. Each produces a bool:
int seats = 5;
int capacity = 24;
bool isFull = seats >= capacity;
bool hasSpace = seats < capacity;
Binary floating-point arithmetic can produce a value extremely close to the expected result without being exactly equal. Compare the gap to a tolerance:
double expected = 0.3;
double actual = 0.1 + 0.2;
double tolerance = 0.000001;
bool closeEnough =
Math.Abs(actual - expected) < tolerance;
The tolerance should match the domain’s acceptable error.
5. Boolean and short-circuit operators
The Boolean operators combine or reverse conditions:
- ! reverses a Boolean value.
- && is true only when both sides are true.
- || is true when at least one side is true.
- & and | evaluate both operands and are not the usual choice for ordinary conditionals.
Short-circuiting can skip unnecessary or unsafe work:
string? text = Console.ReadLine();
bool hasText =
text != null && text.Length > 0;
bool isAdmin = false;
bool allowed =
isAdmin || text == "approved";
With &&, a false left side determines the result. With ||, a true left side determines the result. This is both a performance rule and a correctness rule.
6. Expression types and conversions
An expression has both a value and a type. Operand types select the operator version:
var whole = 7 / 2; // int, value 3
var fractional = 7.0 / 2; // double, value 3.5
var money = 7m / 2; // decimal, value 3.5
A widening conversion can preserve all source values and may occur implicitly. A narrowing conversion can lose information and requires an explicit cast:
int count = 10;
double precise = count;
double average = 92.8;
int wholeNumber = (int)average;
A cast documents intent but does not restore discarded information. C# provides no numeric conversion path to or from bool.
7. Operator overloading
Built-in numeric types have language-defined operator meanings. A class or struct can define selected operators when the meaning is coherent and unsurprising:
public class AddOne
{
public int X;
public static AddOne operator +(AddOne left, AddOne right)
{
return new AddOne { X = left.X + right.X + 1 };
}
}
Operator overloading is a design choice, not a shortcut. Use it only when the operator expresses a natural operation for the type. Otherwise, a named method is clearer.
Classroom application
Build a pricing rule with a decimal price, integer quantity, subtotal, Boolean discount condition, and final total:
decimal price = 19.95m;
int quantity = 3;
decimal subtotal = price * quantity;
bool discount = subtotal >= 50m;
decimal total = discount
? subtotal * 0.90m
: subtotal;
Console.WriteLine($"Total: {total:C}");
Ask students to predict the subtotal, condition, and formatted output before running the program. Then change quantity and explain which expression changes first.
Common misconceptions
- int divided by int is integer division even when assigned to double later.
- Parentheses improve readability and override precedence.
- = assigns and == compares.
- Prefix and postfix increment differ when their value is used.
- Floating-point equality may fail because of binary representation.
- && and || can skip their right operands; & and | do not short-circuit.
- A cast makes a conversion explicit but cannot restore lost information.
- Expression type comes from operands and operator rules.
- Operator overloading should preserve a familiar, well-defined meaning.
Lesson summary
Operators connect typed data to computation and decisions. Students can now trace arithmetic, make evaluation order visible, distinguish integer and fractional division, build Boolean conditions, compare approximate values with a tolerance, and reason about expression types before assignment. These ideas prepare students for conditional statements and program flow.
Course Notes