Operators

By the end of this lesson you'll be able to do maths in C#, compare values, combine conditions with AND/OR, write compact decisions with the ternary operator, and handle missing (null) values safely — the toolkit every decision in your programs is built from.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free C# course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Operators are the buttons on a calculator . Arithmetic operators (+, -, ×, ÷) do maths. Comparison operators are a referee making yes/no calls — "Is player A's score higher than player B's?" Logical operators are how you combine conditions on a checklist: "Do I have BOTH a passport AND a visa?" ( && ) or "Do I have EITHER cash OR a card?" ( || ). The ternary operator is the quick "if yes do this, otherwise do that" sticky note. Master these and you can express almost any decision a program needs to make.

Like maths, C# evaluates some operators before others. Higher priority happens first; () always wins, so when in doubt, add brackets.

1. Arithmetic Operators

Arithmetic operators do the maths: + , - , * , / , and % (remainder). The one that trips up every beginner is integer division : when both numbers are whole, 10 / 3 gives 3 — the decimal part is thrown away, not rounded. The % operator gives you what's left over (handy for "is this even?"). For tidy updates, ++ / -- change a value by one, and compound operators like += mean "add to what's already there". Read this worked example, run it, then you'll write your own.

Your turn. The program below tallies a shopping basket — fill in the four blanks marked ___ using the hints, then run it and check the expected output.

2. Comparison & Logical Operators

A comparison asks a yes/no question and gives back a bool — true or false . You have == (equal), != (not equal), and . Logical operators glue those answers together: && (AND) is true only when both sides are true, || (OR) is true when at least one side is, and ! (NOT) flips a value. These are the foundation of every if statement you'll write next lesson.

Now you build a real rule. A ride needs the person to be old enough and tall enough — both must pass. Fill in the three blanks:

3. Ternary & Null-Safe Operators

The ternary operator condition ? a : b is a one-line if/else that produces a value : "if the condition is true use a , otherwise b ". C# also has null-safe operators for values that might be missing: ?? supplies a fallback when something is null , ??= assigns only if the variable is currently null, and ?. reads a member without crashing on null. (A null value means "nothing here yet" — reading from it directly throws a NullReferenceException , the most common runtime crash in C#.)

Short-circuit evaluation means C# stops the moment the answer is certain. With && , if the left side is false the whole thing is false , so the right side is never run . With || , if the left side is true the right side is skipped. This is not just an optimisation — you rely on it to stay safe, e.g. only touches order.Total once you know order isn't null.

Precedence decides who runs first when you don't add brackets. * and / beat + and - (just like maths), comparisons beat && , and && beats || . When a line gets busy, brackets make your intent obvious and remove all doubt.

Here's a small but real program that uses arithmetic ( % ), comparison, logical OR, and a chained ternary together. Read it line by line — you understand every operator in it now.

A chained ternary reads like a ladder: the first matching condition wins. age 13 ? 6m : age 18 || isStudent ? 8m : 12m checks youngest first, then teen-or-student, then falls through to the standard price.

Q: Why did 10 / 3 give me 3 instead of 3.33 ?

Both numbers are int , so C# does integer division and drops the remainder. Make one side a decimal type — 10.0 / 3 or (double)10 / 3 → 3.333... .

One = assigns a value ( x = 5 puts 5 into x). Two == compares and returns a bool ( x == 5 asks "is x equal to 5?"). Using = in an if is a classic bug — C# usually catches it with a compile error.

Q: When should I use the ternary operator instead of an if/else?

Use the ternary when you're choosing a value in one short expression, like age 18 ? "Adult" : "Minor" . If the branches do several things or get long, a full if/else stays more readable.

Q: What does % actually do, and why is it useful?

It gives the remainder after division. 7 % 2 is 1 . The classic trick is n % 2 == 0 to test if a number is even, or i % 10 == 0 to do something every tenth time.

No blanks this time — just a brief and a blank canvas (with an outline to keep you on track). Combine the remainder operator % with a ternary to decide the answer, run it, and check your output against the examples in the comments.

Practice quiz

What does the % (remainder) operator give for 7 % 2 ?

  • 3
  • 0
  • 1
  • 3.5

Answer: 1. % returns the remainder after dividing; 7 divided by 2 leaves 1.

What is the value of 10 / 3 when both are int?

  • 3
  • 3.33
  • 4
  • 3.0

Answer: 3. Integer division drops the decimal part, so 10 / 3 is 3.

What does the expression true && false evaluate to?

  • true
  • an error
  • null
  • false

Answer: false. && (AND) is true only when both sides are true; here one side is false, so the result is false.

Which operator is logical OR — true when at least one side is true?

  • &&
  • ||
  • !
  • ==

Answer: ||. || (OR) is true when at least one operand is true.

What is the difference between = and == ?

  • = assigns a value, == compares for equality
  • They are identical
  • = compares, == assigns
  • == assigns, = compares

Answer: = assigns a value, == compares for equality. A single = assigns; a double == compares two values and returns a bool.

For int x = 5; what does the expression x++ evaluate to?

  • 6
  • 4
  • 5
  • an error

Answer: 5. Post-increment x++ uses the OLD value (5) in the expression, then adds 1 afterwards.

What does the null-coalescing expression null ?? "Guest" produce?

  • null
  • "Guest"
  • an error
  • false

Answer: "Guest". ?? returns the left side unless it is null, in which case it returns the fallback "Guest".

What does the ternary age >= 18 ? "Adult" : "Minor" return when age is 20?

  • "Minor"
  • true
  • 20
  • "Adult"

Answer: "Adult". 20 >= 18 is true, so the ternary yields the first value, "Adult".

With short-circuit evaluation, in false && Something() what happens?

  • Something() always runs
  • Something() is never run
  • It throws an error
  • It returns true

Answer: Something() is never run. && stops as soon as the left side is false, so the right side (Something()) is never evaluated.

In 2 + 3 * 4 , which operation happens first by precedence?

  • 2 + 3
  • left to right always
  • 3 * 4
  • 4 + 2

Answer: 3 * 4. * has higher precedence than +, so 3 * 4 runs first, giving 2 + 12 = 14.