<style> .reveal-up, .reveal-left, .reveal-right, .reveal-stagger > * { opacity: 1 !important; transform: none !important; } </style>
  1. Learn
  2. C#
  3. Getting started
All lessons in this course

Lesson 5 of 60 13 minBeginner

Operators and expressions

Arithmetic, comparison, logical and assignment operators, integer division, remainders, precedence and increment.

An operator is a symbol that does something with values, like + or >. An expression is any piece of code that produces a value: price * quantity, age >= 18, or just 5.

Try the operators

Interactive

Operator playground

Change the numbers, the type and the operator. Try 7 / 2 as int and as double, and dividing by 0.

int a = 7, b = 2;
var result = a / b;
result3int
Both operands are int, so this is integer division: the decimal part is thrown away (7 / 2 = 3.5 → 3). Make one side a double to keep it.

Arithmetic operators

OperatorMeaningExampleResult
+Add (also joins strings)7 + 29
-Subtract7 - 25
*Multiply7 * 214
/Divide7 / 2 · 7.0 / 23 · 3.5
%Remainder (modulus)7 % 21
Average.cs
int total = 245;int count = 3; double wrong = total / count;          // 81 (int division first!)double right = (double)total / count;  // 81.666... Console.WriteLine(wrong);Console.WriteLine(right.ToString("F2"));
Output
81
81.67

Comparison operators give a bool

OperatorMeaning
==equal to (two equals signs!)
!=not equal to
< >less than, greater than
<= >=less/greater than or equal

Logical operators combine conditions

OperatorNameTrue when…
&&ANDboth sides are true
||ORat least one side is true
!NOTflips true ↔ false
Program.cs
int age = 22;bool hasId = true; bool canEnter = age >= 18 && hasId;      // truebool discount = age < 12 || age > 60;    // falsebool isMinor = !(age >= 18);             // false Console.WriteLine($"{canEnter} {discount} {isMinor}");
Output
True False False

&& and || are short-circuit: if the left side already decides the answer, the right side is not evaluated. x != 0 && 10 / x > 1 is safe even when x is 0.

Assignment shortcuts and ++ / --

Program.cs
int score = 10;score += 5;   // score = score + 5   → 15score -= 3;   // → 12score *= 2;   // → 24score /= 4;   // → 6score %= 4;   // → 2score++;      // add 1 → 3score--;      // subtract 1 → 2 int a = 5;int b = a++;  // b = 5, then a becomes 6 (post-increment)int c = ++a;  // a becomes 7 first, then c = 7 (pre-increment)Console.WriteLine($"{score} {a} {b} {c}");
Output
2 7 5 7

Precedence: which runs first?

Like maths: *, /, % before + and -; then comparisons; then &&; then ||; assignment last. When in doubt, add brackets. They make code clearer even when not required.

Precedence.cs
int r1 = 2 + 3 * 4;      // 14, not 20int r2 = (2 + 3) * 4;    // 20bool ok = 5 > 3 && 2 > 1 || false;  // (true && true) || false → true
Quick check

What is 17 % 5?

Quick check

With int a = 9, b = 2;, what is a / b?

Practice

Given int totalMinutes = 135;, print it as hours and minutes, like “2 h 15 min”.

Show a hint

Hours = totalMinutes / 60 (integer division). Minutes = the remainder.

Show the solution
Solution.cs
int totalMinutes = 135;int hours = totalMinutes / 60;int minutes = totalMinutes % 60;Console.WriteLine($"{hours} h {minutes} min");
Output
2 h 15 min

Remember

  • int / int is integer division; cast one side to double for decimals.
  • % gives the remainder: great for even/odd checks and splitting units.
  • Comparisons give bool; combine them with &&, ||, ! (short-circuit).
  • = assigns, == compares. Use brackets to make precedence obvious.
0 of 60 lessons completed