Skip to main content
ANVISoftware Solutions
Lesson 7 of 15Beginner12 min

Operators

By the end of this lesson

Combine and compare values, and predict the result of integer division.

Operators are the symbols that combine or compare values. Most behave exactly as you expect from arithmetic. Two do not, and those two cause a disproportionate share of beginner bugs.

Arithmetic
C#
int a = 17;
int b = 5;

Console.WriteLine(a + b);   // 22
Console.WriteLine(a - b);   // 12
Console.WriteLine(a * b);   // 85
Console.WriteLine(a / b);   // 3   <- not 3.4
Console.WriteLine(a % b);   // 2   <- the remainder
Getting a fractional result
C#
int total = 17;
int count = 5;

decimal average = (decimal)total / count;   // 3.4
Console.WriteLine(average);
  • (decimal) converts total before the division, so the division is done in decimal rather than whole numbers.
  • Converting only one side is enough — the other is promoted automatically.

The remainder operator %, sometimes called modulo, is more useful than it first appears. It answers "what is left over?", which makes it the standard way to test divisibility.

Remainder in practice
C#
int number = 14;

bool isEven = number % 2 == 0;          // true
bool divisibleBySeven = number % 7 == 0; // true

Console.WriteLine($"{number} even? {isEven}");

Comparison and logic

Comparisons produce a bool — true or false:

==
Equal to. Note the two signs; a single = assigns instead of compares.
!=
Not equal to.
< > <= >=
Less than, greater than, and their inclusive forms.
&&
And — true only when both sides are true.
||
Or — true when at least one side is true.
!
Not — flips true to false and back.
Combining conditions
C#
int age = 34;
bool hasAccount = true;

bool canProceed = age >= 18 && hasAccount;
bool needsAttention = age < 18 || !hasAccount;

Console.WriteLine($"Proceed: {canProceed}, attention: {needsAttention}");

Summary

  • Integer division truncates — it discards the remainder rather than rounding
  • Convert one side to decimal when you want a fractional result
  • % gives the remainder and is the standard divisibility test
  • == compares while = assigns, and comparisons cannot be chained as in mathematics

Practice

Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.

Try it yourself

Try it yourself

A box holds 12 items. Given a total of 100 items, work out how many full boxes you can fill and how many items are left over. Print both.

Show solution

Whole-number division gives the number of full boxes, and the remainder operator gives the leftovers. This pairing comes up constantly — pagination works the same way.

C#
int totalItems = 100;
int itemsPerBox = 12;

int fullBoxes = totalItems / itemsPerBox;   // 8
int leftOver = totalItems % itemsPerBox;    // 4

Console.WriteLine($"{fullBoxes} full boxes, {leftOver} items left over.");

Challenge

Challenge

Work out how many boxes you need to hold all 100 items, including a partly filled final box. Do it without an if statement.

Show solution

Adding one less than the box size before dividing forces the division to round up. This is a standard trick worth recognising — it appears wherever pages, batches or chunks are counted.

C#
int totalItems = 100;
int itemsPerBox = 12;

int boxesNeeded = (totalItems + itemsPerBox - 1) / itemsPerBox;   // 9

Console.WriteLine($"{boxesNeeded} boxes needed.");

Knowledge check

Nothing is recorded and there is no score. The explanation appears either way.

What is the value of `9 / 4` and `9 % 4` in C# when both operands are int?

Saved in this browser only.