0Pricing
C# Academy · Lesson

Arithmetic and Precedence

Operators and order of evaluation.

Arithmetic and Precedence is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Arithmetic Operators

C# provides the familiar arithmetic operators:

  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % remainder (modulo)

They work on every numeric type.

int a = 10, b = 3;
Console.WriteLine(a + b);
Console.WriteLine(a - b);
Console.WriteLine(a * b);

Integer Division

When both operands are integers, / performs integer division and discards the fractional part.

So 7 / 2 is 3, not 3.5. This is a common source of beginner bugs.

int result = 7 / 2;
Console.WriteLine(result);

Getting a Decimal Result

To keep the fraction, make at least one operand a floating-point or decimal value.

You can add .0 to a literal or cast a variable to double.

double result = 7.0 / 2;
double other = (double)7 / 2;
Console.WriteLine(result);
Console.WriteLine(other);

The Modulo Operator

The % operator returns the remainder of a division.

It is handy for checking even or odd numbers, wrapping around ranges, or grouping items.

Console.WriteLine(10 % 3);
Console.WriteLine(8 % 2);
bool isEven = 8 % 2 == 0;
Console.WriteLine(isEven);

Operator Precedence

Operators follow a precedence order, just like in math. Multiplication, division, and modulo bind tighter than addition and subtraction.

So 2 + 3 * 4 equals 14, because 3 * 4 runs first.

Console.WriteLine(2 + 3 * 4);
Console.WriteLine(20 - 6 / 2);

Parentheses Override Precedence

Use parentheses to force a different order of evaluation. Anything inside them is computed first.

Parentheses also make your intent clearer, even when they are not strictly required.

Console.WriteLine((2 + 3) * 4);
Console.WriteLine(10 - (2 + 3));

Left-to-Right Evaluation

When operators share the same precedence, C# evaluates them left to right.

So 10 - 4 - 2 means (10 - 4) - 2, which is 4.

Console.WriteLine(10 - 4 - 2);
Console.WriteLine(24 / 4 / 2);

Unary Operators

The unary minus - negates a value, and unary plus + leaves it unchanged.

Unary operators have very high precedence, applying before binary arithmetic.

int x = 5;
Console.WriteLine(-x);
Console.WriteLine(-x + 3);

Increment and Decrement

++ adds one and -- subtracts one. Placed before a variable (prefix) the change happens first; placed after (postfix) the original value is used first.

int i = 5;
Console.WriteLine(i++);
Console.WriteLine(i);
Console.WriteLine(++i);

Compound Assignment

Compound operators combine arithmetic with assignment: +=, -=, *=, /=, and %=.

So total += 5 is shorthand for total = total + 5.

int total = 10;
total += 5;
total *= 2;
Console.WriteLine(total);

Putting It Together

Real formulas combine several operators. Knowing precedence lets you predict the result without testing it.

Below, multiplication runs before addition, and the cast turns integer division into a decimal average.

int sum = 20 + 30 + 25;
double average = (double)sum / 3;
Console.WriteLine(average);

Quick Check

Apply what you learned about precedence.

Recap

You practiced the arithmetic operators + - * / %, saw how integer division truncates, and learned to get decimal results with casting.

You also learned operator precedence: * / % before + -, left-to-right for equal precedence, and parentheses to take control. Compound operators like += keep code concise.

Frequently asked questions

Is the “Arithmetic and Precedence” lesson free?

Yes — the full text of “Arithmetic and Precedence” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arithmetic and Precedence”?

Operators and order of evaluation. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arithmetic and Precedence” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Numeric Types
  2. Arithmetic and Precedence
  3. The Math Class
  4. Converting Between Numbers
← Back to C# Academy