C-SHARP - 2.8 Operators
C# offers several categories of operators. We will look at four of them here: arithmetic, comparison, assignment and logical. The fifth category, binary operators, is more advanced and is covered later. Arithmetic operators are the ones you already know from school maths: + for addition, - for subtraction, * for multiplication, / for division and % — the modulo — which returns the remainder of a division. The increment ++ and decrement -- operators add or subtract one from a variable; a++ is shorthand for a = a + 1.
Comparison operators test relationships between values. == tests equality (a single = is the assignment operator from earlier videos), != tests inequality, > and < test strict inequality, >= and <= test inequality with equality. Assignment operators include the basic = plus compound shortcuts: a += 5 is the same as a = a + 5, and the same idea applies for -=, *=, /= and %=.
Logical operators (mostly used in conditions)
&&— AND: both conditions must be true. If the batter is ready AND the oven is at 180°C, put it in the oven.||— OR: at least one condition must be true. If the batter is ready OR the oven is at 180°C, put it in the oven.!— NOT: inverts a condition. If!(oven == 180), put it in the oven means "if the oven is NOT at 180°C".
You will use these operators non-stop once we tackle conditions and loops. The arithmetic ones build values, the comparison ones drive decisions, the assignment shortcuts keep the code concise, and the logical ones compose multiple checks together. With this foundation in place, the upcoming exercises will let you combine them in real conditions and see them in action.
Summary
This lesson covers the five main types of operators in C#: arithmetic operators (+, -, *, /, %, ++, --) for mathematical calculations; comparison operators (==, !=, >, >=, <, <=) for testing variable relationships; assignment operators (=, +=, -=, etc.) for assigning and modifying values; and logical operators (&&, ||, !) for combining conditions. Understanding these operators is fundamental to writing conditional logic and performing calculations in C# programs.
Key points
- Arithmetic operators include basic math operations (addition, subtraction, multiplication, division) plus modulo (%) to get the remainder and increment/decrement operators (++, --) as shortcuts to add or subtract 1 from a variable
- Comparison operators use double equals (==) for equality testing and single equals (=) for assignment—this distinction is critical in C#
- Assignment operators like += and -= are shortcuts that combine assignment with an arithmetic operation (e.g., a += 1 equals a = a + 1)
- Logical AND operator (&&) requires both conditions to be true, while OR operator (||) requires only one condition to be true
- The NOT operator (!) inverts a condition, allowing you to express the opposite logic in your conditionals
- These operators are essential building blocks for creating conditions and calculations that control program flow
FAQ
What is the difference between a single equals (=) and double equals (==) in C#?
A single equals (=) is the assignment operator used to assign a value to a variable. Double equals (==) is a comparison operator used to test if two values are equal. Using the wrong operator can cause logic errors in your code.
When should I use the AND (&&) operator versus the OR (||) operator?
Use AND (&&) when you need all conditions to be true before executing code (e.g., 'if preparation is ready AND oven is at 180°'). Use OR (||) when you need at least one condition to be true (e.g., 'if preparation is ready OR oven is at 180°').
What does the modulo operator (%) do?
The modulo operator (%) returns the remainder of a division. For example, 10 % 3 would return 1 because 10 divided by 3 is 3 with a remainder of 1.