Lesson 4: Arithmetic & Assignment Operators in C#
Operators are symbols that tell the compiler to perform specific operations on values. In this lesson, we'll cover the math operators you'll use every day โ and the assignment shortcuts that make your code cleaner and faster to write.
Arithmetic Operators
C# supports all standard math operations. Here's a full reference:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 10 + 3 | 13 |
- | Subtraction | 10 - 3 | 7 |
* | Multiplication | 10 * 3 | 30 |
/ | Division | 10 / 3 | 3 (integer!) |
% | Modulus (remainder) | 10 % 3 | 1 |
int a = 10, b = 3;
Console.WriteLine(a + b); // 13
Console.WriteLine(a - b); // 7
Console.WriteLine(a * b); // 30
Console.WriteLine(a / b); // 3 โ integer division drops remainder
Console.WriteLine(a % b); // 1 โ remainder of 10 รท 3
// For true decimal division, use double or cast first:
double result = (double)a / b;
Console.WriteLine(result); // 3.3333...
๐ก Modulus trick: n % 2 == 0 checks if n is even. n % 2 == 1 checks if it's odd. Very common in loops and algorithms!
String Concatenation with +
The + operator also joins strings. But mixing types requires care:
string first = "Hello";
string last = "World";
Console.WriteLine(first + " " + last); // Hello World
// Mixing string and int with +
int score = 95;
Console.WriteLine("Score: " + score); // Score: 95
// Preferred: string interpolation is cleaner
Console.WriteLine($"Score: {score}"); // Score: 95
Increment & Decrement Operators
These operators add or subtract 1. They can be placed before (prefix) or after (postfix) the variable โ the difference matters:
int x = 5;
// Postfix: returns current value THEN increments
Console.WriteLine(x++); // Output: 5 (x becomes 6 after)
Console.WriteLine(x); // Output: 6
// Prefix: increments THEN returns new value
Console.WriteLine(++x); // Output: 7 (x is 7 before printing)
// Decrement works the same way
int y = 10;
y--; // y is now 9
--y; // y is now 8
Assignment Operators
Assignment operators combine an arithmetic operation with assignment โ shorthand that reduces repetition:
| Operator | Equivalent to | Example (x = 10) | Result |
|---|---|---|---|
= | assign | x = 5 | x = 5 |
+= | x = x + n | x += 3 | x = 13 |
-= | x = x - n | x -= 3 | x = 7 |
*= | x = x * n | x *= 3 | x = 30 |
/= | x = x / n | x /= 3 | x = 3 |
%= | x = x % n | x %= 3 | x = 1 |
int total = 100;
total += 50; // total = 150
total -= 25; // total = 125
total *= 2; // total = 250
total /= 5; // total = 50
total %= 7; // total = 1 (50 mod 7 = 1)
Console.WriteLine(total); // 1
Operator Precedence
When multiple operators appear in one expression, C# evaluates them in a specific order โ just like school math (BODMAS/PEMDAS):
int result = 2 + 3 * 4; // = 2 + 12 = 14 (* before +)
int forced = (2 + 3) * 4; // = 5 * 4 = 20 (parentheses first)
int mixed = 10 - 4 / 2 + 1; // = 10 - 2 + 1 = 9
Console.WriteLine(result); // 14
Console.WriteLine(forced); // 20
Console.WriteLine(mixed); // 9
๐ก Tip: When in doubt, use parentheses to make your intent explicit. It makes code more readable and avoids precedence bugs.
๐ง Quick Check โ Lesson 4
What is the output of: int x = 5; Console.WriteLine(x++);
Lesson Summary
C# has 5 arithmetic operators: +, -, *, /, % (modulus/remainder).
Integer division drops the decimal โ cast to double if you need the fractional result.
x++ returns current value then increments; ++x increments first then returns new value.
Assignment operators like +=, -=, *= are shorthand for common patterns.
* and / have higher precedence than + and -. Use parentheses to override.