Assignment and Comparison Operators
In this lesson, you'll learn about operators for assigning values to variables and comparing values.
Assignment Operators
The basic assignment operator = stores a value in a variable:
csharp1int x = 10; // Assign 10 to x2string name = "Alice"; // Assign "Alice" to name
Compound Assignment Operators
Combine an operation with assignment:
| Operator | Example | Equivalent |
|---|---|---|
+= | x += 5 | x = x + 5 |
-= | x -= 3 | x = x - 3 |
*= | x *= 2 | x = x * 2 |
/= | x /= 4 | x = x / 4 |
%= | x %= 3 | x = x % 3 |
csharp1int score = 100;23score += 10; // score is now 1104Console.WriteLine(score);56score -= 20; // score is now 907Console.WriteLine(score);89score *= 2; // score is now 18010Console.WriteLine(score);1112score /= 3; // score is now 6013Console.WriteLine(score);
String Concatenation Assignment
csharp1string message = "Hello";2message += " World"; // message is now "Hello World"3Console.WriteLine(message);45// Building a string6string result = "";7result += "Line 1\n";8result += "Line 2\n";9result += "Line 3";10Console.WriteLine(result);
Comparison Operators
Comparison operators compare two values and return a boolean (true or false):
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | true |
!= | Not equal to | 5 != 3 | true |
> | Greater than | 10 > 5 | true |
< | Less than | 3 < 7 | true |
>= | Greater than or equal | 5 >= 5 | true |
<= | Less than or equal | 4 <= 3 | false |
csharp1int a = 10;2int b = 5;34Console.WriteLine($"a == b: {a == b}"); // false5Console.WriteLine($"a != b: {a != b}"); // true6Console.WriteLine($"a > b: {a > b}"); // true7Console.WriteLine($"a < b: {a < b}"); // false8Console.WriteLine($"a >= b: {a >= b}"); // true9Console.WriteLine($"a <= b: {a <= b}"); // false
Common Mistake: = vs ==
csharp1int x = 5;23// Assignment (sets x to 10)4x = 10;56// Comparison (checks if x equals 10)7bool isEqual = x == 10; // true
Comparing Different Types
csharp1// Integers2int score1 = 85;3int score2 = 90;4bool isHigher = score2 > score1; // true56// Doubles7double price1 = 19.99;8double price2 = 20.00;9bool isCheaper = price1 < price2; // true1011// Characters (compared by ASCII value)12char a = 'A'; // ASCII 6513char b = 'B'; // ASCII 6614bool result = a < b; // true
String Comparison
Strings have special comparison rules:
csharp1string name1 = "Alice";2string name2 = "Alice";3string name3 = "alice";45// == compares string content6Console.WriteLine(name1 == name2); // true7Console.WriteLine(name1 == name3); // false (case-sensitive)89// Case-insensitive comparison10bool sameIgnoreCase = string.Equals(name1, name3,11 StringComparison.OrdinalIgnoreCase); // true1213// Comparing with null14string empty = null;15Console.WriteLine(name1 == null); // false16Console.WriteLine(empty == null); // true
String Ordering
csharp1string a = "apple";2string b = "banana";34// CompareTo returns: negative if less, 0 if equal, positive if greater5int comparison = a.CompareTo(b); // negative (a comes before b)67// String.Compare8int result = string.Compare("ABC", "abc"); // depends on culture9int resultIgnoreCase = string.Compare("ABC", "abc",10 StringComparison.OrdinalIgnoreCase); // 0 (equal)
Storing Comparison Results
csharp1int age = 25;2int requiredAge = 18;34// Store comparison result in a boolean5bool isAdult = age >= requiredAge;6Console.WriteLine($"Is adult: {isAdult}"); // Is adult: True78// Use in expressions9string status = isAdult ? "Adult" : "Minor";10Console.WriteLine(status); // Adult
Chaining Comparisons
Unlike some languages, C# doesn't support chaining like 1 < x < 10. Use logical operators instead:
csharp1int x = 5;23// This does NOT work as expected:4// bool inRange = 1 < x < 10; // Compiler error56// Correct approach (using logical AND)7bool inRange = x > 1 && x < 10; // true
Practical Examples
csharp1// Grade checker2int score = 85;3bool isPassing = score >= 60;4bool isExcellent = score >= 90;56Console.WriteLine($"Passing: {isPassing}"); // true7Console.WriteLine($"Excellent: {isExcellent}"); // false89// Price comparison10decimal currentPrice = 49.99m;11decimal budgetLimit = 50.00m;1213bool canAfford = currentPrice <= budgetLimit;14Console.WriteLine($"Can afford: {canAfford}"); // true1516// Temperature check17double temperature = 72.5;18bool isComfortable = temperature >= 68 && temperature <= 76;19Console.WriteLine($"Comfortable: {isComfortable}"); // true
Summary
In this lesson, you learned:
- Assignment operator
=stores values in variables - Compound operators (
+=,-=, etc.) combine operation and assignment - Comparison operators (
==,!=,>,<,>=,<=) return boolean values - String comparison is case-sensitive by default
- Store comparison results in boolean variables for clarity
Next, we'll explore logical operators for combining conditions.