Skip to main content

    Lesson 3 • Beginner Track

    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.

    What You'll Learn

    • Use arithmetic operators (+, -, *, /, %) and understand integer division
    • Apply increment (++), decrement (--), and compound assignment (+=, -=, *=)
    • Compare values with ==, !=, >, <, >=, <= and know they return bool
    • Combine conditions with logical operators (&&, ||, !)
    • Use the ternary operator (? :) for concise conditional values
    • Handle null values safely with ??, ??=, and ?.

    💡 Real-World Analogy

    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.

    📊 Operator Precedence (who goes first)

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

    PriorityOperatorsMeaning
    1 (Highest)() ?. !Parentheses, null-conditional, not
    2* / %Multiply, divide, remainder
    3+ -Add, subtract
    4< > <= >=Comparison
    5== !=Equality
    6&&Logical AND
    7||Logical OR
    8?:Ternary
    9 (Lowest)= += -= *= ??=Assignment

    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.

    Worked example: arithmetic, ++/-- and compound assignment

    Read every comment, run it, and check the output matches.

    Try it Yourself »
    C#
    using System;
    
    class Program
    {
        static void Main()
        {
            int a = 10, b = 3;
    
            // Basic arithmetic — each line states its result on the right.
            Console.WriteLine($"a + b = {a + b}");   // 13
            Console.WriteLine($"a - b = {a - b}");   // 7
            Console.WriteLine($"a * b = {a * b}");   // 30
            Console.WriteLine($"a / b = {a / b}");   // 3  (integer division — decimals dropped!)
            Console.WriteLine($"a % b = {a % b}");   // 1  (% is the REMAINDER after d
    ...

    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.

    🎯 Your turn: total up a basket

    Fill in the ___ blanks, then check your output against the expected lines.

    Try it Yourself »
    C#
    using System;
    
    class Program
    {
        static void Main()
        {
            // 🎯 YOUR TURN — replace each ___ then press "Try it Yourself".
            // You're totalling up a shopping basket.
    
            int applesPrice = 3;   // £ each
            int breadPrice  = 2;   // £ each
            int milkPrice   = 1;   // £ each
    
            // 1) Start a running total at 0
            int total = ___;            // 👉 the starting amount, a whole number
    
            // 2) Add each item to the total using +=
            total += apple
    ...

    2. Comparison & Logical Operators

    A comparison asks a yes/no question and gives back a booltrue 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.

    Worked example: comparison & logical operators

    See how comparisons return bool and how &&, ||, ! combine them.

    Try it Yourself »
    C#
    using System;
    
    class Program
    {
        static void Main()
        {
            // Comparison operators always return a bool (true / false).
            int age = 20;
            Console.WriteLine($"age == 20: {age == 20}");  // true   (== means "equal to")
            Console.WriteLine($"age != 18: {age != 18}");  // true   (!= means "not equal")
            Console.WriteLine($"age > 18:  {age > 18}");   // true
            Console.WriteLine($"age <= 21: {age <= 21}");  // true
    
            // Logical operators combine bool value
    ...

    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:

    🎯 Your turn: can this person ride?

    Build a boolean condition with >= and &&, then check your output.

    Try it Yourself »
    C#
    using System;
    
    class Program
    {
        static void Main()
        {
            // 🎯 YOUR TURN — a theme-park ride has TWO rules:
            //    you must be at least 12 years old AND at least 140 cm tall.
    
            int age = 13;
            int heightCm = 145;
    
            // 1) Is the person old enough? (12 or over)
            bool oldEnough = age >= ___;       // 👉 the minimum age, a whole number
    
            // 2) Is the person tall enough? (140 or over)
            bool tallEnough = heightCm ___ 140; // 👉 the "greater th
    ...

    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#.)

    Worked example: ternary & null-safe operators

    Try changing the values to null and back, then run it.

    Try it Yourself »
    C#
    using System;
    
    class Program
    {
        static void Main()
        {
            // Ternary operator:  condition ? valueIfTrue : valueIfFalse
            // It's a one-line if/else that PRODUCES a value.
            int age = 20;
            string status = age >= 18 ? "Adult" : "Minor";
            Console.WriteLine($"Status: {status}");  // Status: Adult
    
            // Null-coalescing (??) — "use the left side, or this fallback if it's null".
            string? userName = null;
            string displayName = userName ?? "Guest";
    
    ...

    🔎 Deep Dive: short-circuiting & precedence

    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. order != null && order.Total > 0 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.

    int total = 2 + 3 * 4;        // 14  (3*4 first, THEN +2 — not 20)
    bool ok = 5 > 3 && 2 > 1;     // true  (each > runs before &&)
    bool a = true || Crash();     // true  (Crash() never runs — short-circuit)
    bool b = (1 + 2) * 3 == 9;    // true  (() forces the add to go first)

    Putting It Together: a Ticket Pricer

    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.

    Worked example: cinema ticket pricer

    Change the age and isStudent and watch the price and seat type update.

    Try it Yourself »
    C#
    using System;
    
    class Program
    {
        static void Main()
        {
            // === Cinema ticket price — combines this lesson's operators ===
            int age = 15;
            bool isStudent = true;
    
            // % (remainder) tells us if a number is even: even numbers leave 0.
            int seat = 24;
            string seatType = seat % 2 == 0 ? "aisle" : "window";  // "aisle"
    
            // Pick a price with a ternary chain (read top to bottom).
            // Under 13 = £6, 13–17 OR a student = £8, otherwise £12.
        
    ...

    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.

    Pro Tips

    • 💡 Use parentheses for clarity: even when precedence is correct, (a > 5) && (b < 10) reads more clearly than a > 5 && b < 10.
    • 💡 Prefer ?? over an if-null check: string name = input ?? "default"; is cleaner than a 4-line if/else.
    • 💡 Short-circuit evaluation: && stops at the first false and || stops at the first true. Put the cheap or most-likely-to-decide check first.
    • 💡 Use % for "every Nth": i % 2 == 0 is even; i % 5 == 0 is every fifth item.
    • 💡 Chain ?. for nested data: user?.Address?.City returns null safely instead of crashing.

    Common Errors (and the fix)

    • "CS0029: Cannot implicitly convert type 'int' to 'bool'": you wrote = (assign) where you meant == (compare). if (x = 5) is wrong; use if (x == 5).
    • Integer division surprise: 7 / 2 gives 3, not 3.5. Make one side a decimal: (double)7 / 2 or 7.0 / 23.5.
    • Confusing ++ placement: x++ uses the old value then adds 1; ++x adds 1 first. They print different things inside an expression.
    • "CS0019: Operator '<' cannot be applied": you tried to chain comparisons like maths — 1 < x < 10 doesn't compile. Write x > 1 && x < 10.
    • "CS8602: Dereference of a possibly null reference": you read .Length on something that might be null. Use text?.Length or give a fallback with ?? — don't silence it with !.

    📋 Quick Reference

    OperatorDoesExampleResult
    %Remainder7 % 21
    /Integer divide7 / 23
    +=Add & assignn += 5n grows by 5
    ==Equal?3 == 3true
    &&AND (both)true && falsefalse
    ||OR (either)true || falsetrue
    ?:Ternaryx > 0 ? "+" : "-""+" or "-"
    ??Null fallbackname ?? "Guest"name or "Guest"

    Frequently Asked Questions

    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 / 33.333....

    Q: What's the difference between = and ==?

    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.

    Mini-Challenge: Even or Odd?

    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.

    🎯 Mini-Challenge: is the number even?

    Use % and a ternary to print whether your number is even or odd.

    Try it Yourself »
    C#
    using System;
    
    class Program
    {
        static void Main()
        {
            // 🎯 MINI-CHALLENGE: Even or odd?
            // 1. Create an int "number" set to any whole number (try 7, then 10).
            // 2. Use the remainder operator %  ->  number % 2 is 0 for even numbers.
            // 3. Use a TERNARY to pick the word "even" or "odd".
            // 4. Print: "7 is odd"  (using your number and the chosen word).
            //
            // ✅ Expected (number = 7):  7 is odd
            // ✅ Expected (number = 10): 10 is 
    ...

    🎉 Lesson Complete

    • ✅ Arithmetic: +, -, *, /, % — and integer division truncates
    • ✅ Increment/decrement: x++ (post) vs ++x (pre) return different values
    • ✅ Compound assignment: +=, -=, *= update a variable in place
    • ✅ Comparison operators return bool: ==, !=, >, <, >=, <=
    • ✅ Logical: && (AND), || (OR), ! (NOT) — with short-circuiting
    • ✅ Ternary condition ? a : b; null-safe ??, ??=, ?.
    • Next lesson: Control Flow — turn these conditions into if, switch, and pattern matching

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service