Welcome back! On Day 2 you learned how to put stuff in boxes (variables). Today we're going to smash those boxes together to do math, compare things, and make decisions. This is where your code stops being a glorified sticky note and starts acting like an actual program.
1. What Is an Operator, Really?
An operator is a symbol that tells C# to do something with one or more values. An expression is any combination of values and operators that C# can evaluate down to a single result.
Think of operators as tiny kitchen appliances. A blender (+) takes two smoothies and makes one. A scale (==) takes two watermelons and tells you if they weigh the same. An expression is the whole recipe.
int total = 5 + 3; // "5 + 3" is an expression, it evaluates to 8
bool isAdult = age >= 18; // "age >= 18" is an expression, it evaluates to true or false
2. Arithmetic Operators (Your Calculator is Back)
The five basics you already know from fourth grade, but with one gotcha:
int a = 10;
int b = 3;
Console.WriteLine(a + b); // 13 (addition)
Console.WriteLine(a - b); // 7 (subtraction)
Console.WriteLine(a * b); // 30 (multiplication)
Console.WriteLine(a / b); // 3 (division — WAIT WHAT)
Console.WriteLine(a % b); // 1 (remainder / modulo)
The integer division trap: 10 / 3 is 3, not 3.333.... When you divide two int values, C# throws away the decimal part without asking. If you want decimals, at least one side must be a double:
double result = 10.0 / 3; // 3.3333333333333335
The % (modulo) operator gives you the leftover after division. It's shockingly useful: n % 2 == 0 tells you if a number is even.
3. The Assignment Family
You already met = (plain assignment). It has a bunch of cousins that do math and assign in one shot:
| Operator | Does | Same as |
|---|---|---|
+= |
Add and assign | x = x + 5 |
-= |
Subtract and assign | x = x - 5 |
*= |
Multiply and assign | x = x * 5 |
/= |
Divide and assign | x = x / 5 |
%= |
Modulo and assign | x = x % 5 |
int score = 10;
score += 5; // score is now 15
score *= 2; // score is now 30
And the weird twins — ++ and --:
int count = 0;
count++; // count is now 1 (same as count = count + 1)
count--; // count is now 0
You'll see these all over loops tomorrow.
4. Comparison Operators (The Judgment Squad)
These ask C# a question and get back a bool — true or false:
| Operator | Question |
|---|---|
== |
Are they equal? |
!= |
Are they NOT equal? |
> |
Is left bigger? |
< |
Is left smaller? |
>= |
Is left bigger or equal? |
<= |
Is left smaller or equal? |
int age = 25;
Console.WriteLine(age == 25); // true
Console.WriteLine(age != 30); // true
Console.WriteLine(age > 18); // true
Console.WriteLine(age < 18); // false
Mega-gotcha: = assigns, == compares. Writing if (age = 18) when you meant if (age == 18) is a classic rite of passage. The compiler will usually catch it, but it'll judge you quietly.
5. Logical Operators (Combining Questions)
When one question isn't enough, you glue them together. C# has three of them:
&&— AND: BOTH sides must betrue.||— OR: AT LEAST ONE side must betrue.!— NOT: flipstruetofalseand vice versa.
int age = 25;
bool hasTicket = true;
bool canEnter = age >= 18 && hasTicket; // true
bool needsHelp = age < 13 || age > 65; // false
bool isMinor = !(age >= 18); // false
C# is also short-circuit: if the left side of && is already false, it doesn't even bother checking the right side. Same idea for || with true. This is not just an optimization — it's how you write safe code like if (user != null && user.IsActive) without blowing up.
6. Operator Precedence (Who Goes First?)
Remember PEMDAS from school? C# has its own version. Here's the short cheat sheet:
()— parentheses first, always!,++,--— unary stuff*,/,%— multiplication-like+,-— addition-like<,>,<=,>=— comparison==,!=— equality&&— AND||— OR=,+=, etc. — assignment last
int result = 2 + 3 * 4; // 14, not 20 — * beats +
int other = (2 + 3) * 4; // 20 — parentheses win
Pro tip: when in doubt, add parentheses. They cost nothing and make your intent obvious to the next human (usually future-you at 2 AM).
For the full precedence table, see the C# operators reference.
7. The Sneaky Ternary Operator
The ?: operator is a one-line if/else for when you need to pick between two values:
int age = 20;
string status = age >= 18 ? "adult" : "minor";
Console.WriteLine(status); // adult
Read it as: "Is age >= 18? If yes, give me "adult". Otherwise, give me "minor"." Don't nest three of these inside each other — your teammates will hunt you down.
8. Your Homework: Build a Tiny Tip Calculator
Ask the user for a bill amount and a tip percentage, then print the tip, the total, and whether they're a "generous" tipper (20% or more).
Console.Write("Bill amount: $");
double bill = double.Parse(Console.ReadLine());
Console.Write("Tip percentage (e.g. 15): ");
double tipPercent = double.Parse(Console.ReadLine());
double tip = bill * (tipPercent / 100);
double total = bill + tip;
bool isGenerous = tipPercent >= 20;
Console.WriteLine($"Tip: ${tip:F2}");
Console.WriteLine($"Total: ${total:F2}");
Console.WriteLine($"Generous tipper? {isGenerous}");
Two new tricks:
double.Parse(...)— same asint.Parse, but for decimals.{tip:F2}— inside string interpolation,:F2means "format as a fixed-point number with 2 decimal places." Very handy for money.
Bonus challenge: use the ternary operator to print "Generous!" or "Stingy..." instead of true/false.
Summary of Day 3
- Operators are symbols that do things; expressions are combinations that evaluate to a value.
- Arithmetic:
+ - * / %— and rememberint / inttruncates. - Assignment shortcuts:
+=,-=,*=,/=,++,--. - Comparisons return
bool:==,!=,>,<,>=,<=. - Logical:
&&(AND),||(OR),!(NOT) — and they short-circuit. - Precedence matters; when in doubt, add parentheses.
- The ternary
?:is a compactif/elsefor picking between two values.
Tomorrow: we'll talk about Control Flow — the if, else, and switch statements that turn all these true/false answers into actual branching decisions. Your code is about to grow a brain. 🧮
See you on Day 4!