Skip to content

Operators and expressions

↑

Examples are independent C# snippets for modern .NET console projects unless noted otherwise. Class declarations and top-level code can live in Program.cs when top-level statements come first.

Operators combine values into expressions. Their precedence matters, but parentheses are usually the clearest way to express intended order.

+, -, *, / and % perform arithmetic. When both operands of / are integers, the result uses integer division. +=, -=, *= and /= update the variable on the left.

Example.cs
int points = 11;
points += 4;
int groups = points / 4; // 3
int remainder = points % 4; // 3
Console.WriteLine($"{groups} groups, {remainder} left");

Comparison operators produce bool: ==, !=, <, <=, > and >=. Combine conditions with && (AND), || (OR) and ! (NOT). && and || short-circuit, meaning C# may not evaluate the right side.

Example.cs
int health = 30;
bool hasShield = true;
bool canContinue = health > 0 && (hasShield || health > 50);
Console.WriteLine(canContinue); // True

Use Math.Min, Math.Max, Math.Abs, Math.Sqrt and Math.Round for common numeric tasks. For float-oriented Unity code you may also encounter Mathf, but that is a different API.

Example.cs
double distance = Math.Sqrt(3 * 3 + 4 * 4);
int clamped = Math.Clamp(125, 0, 100);
Console.WriteLine($"{distance}, {clamped}"); // 5, 100

Deep dive: operator meaning depends on the operands

Section titled “Deep dive: operator meaning depends on the operands”

Multiplication binds more tightly than addition: 2 + 3 * 4 is 14, while (2 + 3) * 4 is 20. Parentheses are useful even when you remember the precedence table because they communicate intent. An expression may also have side effects, such as count++; avoid squeezing several side effects into one hard-to-read line.

When both operands are integers, / performs integer division: 7 / 2 is 3, not 3.5. Cast an operand or use floating-point literals when fractions matter. % produces the remainder, useful for alternating turns, partitioning IDs and wrapping an index around a known length. For negative inputs, C#’s remainder can be negative; do not blindly use it for array indexing.

Program.cs
Console.WriteLine(7 / 2); // 3
Console.WriteLine(7 / 2.0); // 3.5
Console.WriteLine(17 % 5); // 2
Console.WriteLine((2 + 3) * 4); // 20

&& and || short-circuit; & and | can do more

Section titled “&& and || short-circuit; & and | can do more”

&& evaluates its right operand only when the left operand is true. || evaluates the right operand only when the left is false. This lets you safely write name != null && name.Length > 0. For boolean operands, & and | evaluate both sides; for integral operands they perform bitwise operations. Understand which operation you meant, especially when the right side changes data.

score += 10 is a compound assignment, roughly meaning “add and store the result” with language-defined evaluation and conversion rules. count++ returns a value too; the prefix and postfix versions differ if you use that value. For routine updates, use one clear statement instead of clever chains that hide the order of changes.

Math.Clamp(value, min, max) limits a value to a range at the call site, Math.Abs calculates absolute value, and Math.Round uses particular rounding rules (the default midpoint behavior may surprise you). Validate the expected numeric range and rounding mode for scoring, finance and gameplay. In Unity, Mathf offers common float-oriented counterparts.

Use comparisons inside decisions and Math.Clamp when you need a value within an allowed range.

= assigns while == compares; & and | are not always equivalent to short-circuiting && and ||.

Write a condition checking that a level is between 1 and 10 inclusive.

Reveal a solution

level >= 1 && level <= 10.