Read the syntax
Arithmetic operators combine numeric operands; % returns the remainder. With two int operands, / produces an int and discards the fractional part, so choose a floating-point or decimal operand when fractions matter.
Compound assignment such as score += 5 updates a variable using its previous value. Prefix ++x increments before yielding a value; postfix x++ yields the old value before incrementing.
Syntax
+ - * / % += -= ++ --Examples
Basic Arithmetic
The core arithmetic operators, including integer division behavior.
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 truncates)
Console.WriteLine(a % b); // 1 (remainder)
double result = 10.0 / 3; // true division with a double
Console.WriteLine(result); // 3.3333333333333335- The core arithmetic operators, including integer division behavior.
- Arithmetic operators combine numeric operands; % returns the remainder. With two int operands, / produces an int and discards the fractional part, so choose a floating-point or decimal operand when fractions matter.
- The int expressions print 13, 7, 30, 3 and 1. Because 10.0 is a double, 10.0 / 3 keeps a fractional result instead of using integer division.
Compound Assignment Operators
Shorthand operators that update a variable based on its current value.
int score = 10;
score += 5; // score = score + 5 -> 15
score -= 3; // 12
score *= 2; // 24
score /= 4; // 6
Console.WriteLine(score);- Shorthand operators that update a variable based on its current value.
- Arithmetic operators combine numeric operands; % returns the remainder. With two int operands, / produces an int and discards the fractional part, so choose a floating-point or decimal operand when fractions matter.
- Each compound assignment uses the previous score: 10 becomes 15, 12, 24 and finally 6. The single WriteLine prints the final value, not each intermediate one.
Increment and Decrement
The difference between prefix (++x) and postfix (x++) forms.
int x = 5;
Console.WriteLine(x++); // prints 5, THEN increments (x is now 6)
Console.WriteLine(x); // 6
int y = 5;
Console.WriteLine(++y); // increments FIRST, then prints (y is now 6)
Console.WriteLine(y); // 6- The difference between prefix (++x) and postfix (x++) forms.
- Arithmetic operators combine numeric operands; % returns the remainder. With two int operands, / produces an int and discards the fractional part, so choose a floating-point or decimal operand when fractions matter.
- x++ passes 5 to the first WriteLine and then stores 6. ++y stores 6 before the first WriteLine; the later calls confirm both variables end at 6.
Best practices
- Watch out for integer division truncating results - divide by a double or decimal literal when you need a precise fractional result
- Use decimal rather than double for any calculation involving money, to avoid floating-point rounding errors
- Be careful mixing prefix and postfix increment operators within the same complex expression - split them into separate statements for clarity
- Use compound assignment operators (+=, -=, etc.) for conciseness when updating a variable based on itself
Runtime & versions
.NET runtime. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
C# language reference