If today == Wednesday && rainyWeather == false, then it's the perfect time to write a blog post about C# operators!
But what if today != Wednesday || any other day of the week? || weather == rainyWeather? Well, in that case, we could always grab a coffee, wait a little while, and try again.
Of course, we could also check whether coffee > motivation, whether deadline <= procrastination, || whether developer == "me" && coffee >= 2.
And if all of those conditions evaluate to true, then congratulations: you've already started using operators!
So, let's take a look at the little symbols that let us compare, calculate, assign, and make decisions in C#. After all, without operators, our code would have a pretty hard time doing much of anything.
Let's take a closer look, shall we?
What Is The Core Concept Of An Operator?
An operator is a symbol that performs an action on one or more values. Add two numbers, compare two values, combine two conditions, all of that runs through operators. You've already seen plenty of them scattered across variables, loops, and if statements; this article just gathers them all in one place and explains what each one actually does.
value operator value → result
C#'s operators split into a few useful categories: arithmetic (math), comparison (checking relationships between values), and logical (combining true/false conditions). Knowing which bucket an operator belongs to makes the whole system click a lot faster.
Arithmetic Operators: Doing The Math
These are the ones you learned in grade school, just with a couple of extras C# adds on top.
+is addition.health + 10-is subtraction.health - damage*is multiplication.damage * critMultiplier/is division.totalXP / playerCount%is modulo, the remainder left over after division.10 % 3equals1, since 3 goes into 10 three times with 1 left over.
That last one, modulo, confuses beginners constantly since it doesn't come up outside of programming much. It's genuinely useful though, checking if (score % 100 == 0) is a common way to trigger something every 100 points, like a bonus life.
int health = 100;
health = health - 25; // health is now 75
int remainder = 10 % 3; // remainder is 1
C# also gives you shortcuts for the common case of "do math on a variable and store the result back into itself":
health -= 25; // same as health = health - 25
score += 10; // same as score = score + 10
damage *= 2; // same as damage = damage * 2
Comparison Operators: Checking Relationships
These are the backbone of nearly every C# comparison operators situation you'll run into, especially inside if statements. Every single one of these evaluates to either true or false, nothing else.
==is equal to.health == 0!=is not equal to.playerName != "Jimmy">is greater than.score > highScore<is less than.health < 20>=is greater than or equal to.level >= 10<=is less than or equal to.ammo <= 0
if (health <= 0)
{
Debug.Log("Player has died.");
}
The = vs == Mistake: Every Beginner's Rite Of Passage
This is, without exaggeration, one of the most common beginner mistakes in all of C#, and it's worth its own section because of how sneaky it is. A single = assigns a value. A double == compares two values. Mix them up, and the bug that follows can be genuinely confusing to track down.
// WRONG: this assigns 0 to health instead of checking it
if (health = 0)
{
Debug.Log("This will cause a compiler error in C#, thankfully.");
}
// CORRECT: this checks whether health equals 0
if (health == 0)
{
Debug.Log("Player has died.");
}
The good news for C# specifically, unlike some other languages, writing if (health = 0) usually throws a compiler error instead of silently running, because C# expects a bool inside an if condition, and health = 0 evaluates to an int, not a bool. Still, the habit of double-checking for a single = where you meant == is worth building early, since it's one of the single most common common C# mistakes beginners run into.
Logical Operators: Combining Conditions
Sometimes checking one thing isn't enough; you need to combine multiple conditions together. That's exactly what logical operators handle.
&&is AND. Every single condition has to be true for the whole thing to be true.||is OR. Just one condition needs to be true.!is NOT. Flips atrueintofalseand vice versa.
if (isGrounded && Input.GetButtonDown("Jump"))
{
Jump();
}
if (health <= 0 || isOutOfBounds)
{
RespawnPlayer();
}
if (!isPaused)
{
UpdateGameplay();
}
That first example reads almost like a sentence, if the player is grounded AND the jump button was just pressed, then jump. Both have to be true. The second example, using ||, triggers a respawn if either condition is true, health hitting zero OR falling out of bounds, doesn't matter which one happened.
Operator Precedence: Which Runs First?
Just like basic math class, multiplication and division run before addition and subtraction unless you use parentheses to force a different order. The same idea applies to comparison and logical operators too.
int result = 2 + 3 * 4; // 14, not 20, because * runs before +
int correctedResult = (2 + 3) * 4; // 20, parentheses force addition first
When in doubt, just add parentheses. They cost nothing, and they remove any ambiguity about what you actually meant, both for the compiler and for your own sanity when you revisit the code six months later.
So Which Operator Should You Actually Use?
Here's the no-fluff version, the gut check you can run through in your head:
- Performing math on a value → arithmetic operators (
+ - * / %) - Checking if two values match →
==, never a single= - Checking a relationship between values →
> < >= <= - All conditions need to be true →
&& - Just one of several conditions needs to be true →
|| - Flipping a true/false value →
! - Not sure which runs first → wrap it in parentheses
Getting comfortable with C#'s operators isn't really about memorizing a list, it's about recognizing which category you're working in, math, comparison, or logic, and reaching for the right symbol inside that category.
In A Nutshell...
Operators are small, easy to overlook, and genuinely everywhere in your code. Arithmetic handles the math, comparisons handle the true/false checks, and logical operators let you combine those checks into more complex conditions. Keep an eye out for that = vs == trap early on, and the rest of this stuff becomes second nature fast!
~happy Coding!


No comments:
Post a Comment