Chapter 4: Operators

1. What Are Operators? (Super Simple Explanation)

Operators are special symbols that perform actions on one or more values (called operands).

Examples you already know from school:

  • 5 + 3 → + is the operator
  • 10 > 7 → > is the operator

In C#, there are many types of operators. We’ll learn the most important ones today.

2. Arithmetic Operators (Math Operations)

These are used for calculations – just like your calculator.

Operator Name Example Result Notes
+ Addition 5 + 3 8 Also works with strings!
Subtraction 10 – 4 6
* Multiplication 6 * 7 42
/ Division 10 / 3 3 (int) or 3.333… (double) Integer division truncates!
% Modulus (Remainder) 10 % 3 1 Very useful for even/odd checks
+ String Concatenation “Hello” + ” World” “Hello World”

Real code example:

C#

Quick Tip: If both operands are int → result is int (truncates decimals). Want decimal result? → cast at least one to double or decimal.

3. Assignment Operators (Giving Values)

The most common is = – but there are shorthand versions too!

Operator Meaning Example Same as
= Assign x = 5
+= Add and assign x += 3 x = x + 3
-= Subtract and assign x -= 2 x = x – 2
*= Multiply and assign x *= 4 x = x * 4
/= Divide and assign x /= 2 x = x / 2
%= Modulus and assign x %= 3 x = x % 3

Example:

C#

4. Comparison Operators (Make Decisions)

These return true or false (bool).

Operator Meaning Example Result
== Equal to 5 == 5 true
!= Not equal to 5 != 3 true
> Greater than 10 > 7 true
< Less than 3 < 8 true
>= Greater than or equal 5 >= 5 true
<= Less than or equal 4 <= 6 true

Example:

C#

5. Logical Operators (Combine Conditions)

Operator Name Example Meaning
&& AND true && false Both must be true
|| OR true || false At least one must be true
! NOT !true Reverses the value

Example:

C#

6. Increment / Decrement Operators (Very Common!)

Operator Meaning Example Effect
++ Increment by 1 x++ or ++x x = x + 1
Decrement by 1 x– or –x x = x – 1

Difference between prefix (++x) and postfix (x++):

C#

Common use: Loops (we’ll see later!)

7. Null-Coalescing Operators (Modern & Super Useful!)

These help when a value might be null (nothing).

Operator Name Example Meaning
?? Null-coalescing name ?? “Guest” Use left if not null, else use right
??= Null-coalescing assignment name ??= “Guest” Assign right only if left is null
?. Null-conditional person?.Age If person is null → whole expression is null

Example:

C#

Mini-Project: Simple Shopping Cart

C#

Your Homework (Fun & Practical!)

  1. Create a new console project called OperatorPlayground
  2. Declare variables for:
    • Your current bank balance (decimal)
    • Monthly salary (decimal)
    • Number of coffees you drink per day (int)
    • Price per coffee (decimal)
  3. Calculate:
    • Total monthly coffee expense
    • Remaining balance after salary + coffee expense
    • Whether you can afford a new phone (price > remaining balance?)
  4. Use +=, %, ??, ?. at least once

Next lesson: Control Flow (if, else, switch, loops) – we’re going to make programs that make decisions and repeat actions!

You’re doing absolutely fantastic! 🎉 Any operator confusing you? Want more examples? Just ask — I’m right here for you! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *