10 minlesson

Assignment and Comparison Operators

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:

csharp
1int x = 10; // Assign 10 to x
2string name = "Alice"; // Assign "Alice" to name

Compound Assignment Operators

Combine an operation with assignment:

OperatorExampleEquivalent
+=x += 5x = x + 5
-=x -= 3x = x - 3
*=x *= 2x = x * 2
/=x /= 4x = x / 4
%=x %= 3x = x % 3
csharp
1int score = 100;
2
3score += 10; // score is now 110
4Console.WriteLine(score);
5
6score -= 20; // score is now 90
7Console.WriteLine(score);
8
9score *= 2; // score is now 180
10Console.WriteLine(score);
11
12score /= 3; // score is now 60
13Console.WriteLine(score);

String Concatenation Assignment

csharp
1string message = "Hello";
2message += " World"; // message is now "Hello World"
3Console.WriteLine(message);
4
5// Building a string
6string 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):

OperatorDescriptionExampleResult
==Equal to5 == 5true
!=Not equal to5 != 3true
>Greater than10 > 5true
<Less than3 < 7true
>=Greater than or equal5 >= 5true
<=Less than or equal4 <= 3false
csharp
1int a = 10;
2int b = 5;
3
4Console.WriteLine($"a == b: {a == b}"); // false
5Console.WriteLine($"a != b: {a != b}"); // true
6Console.WriteLine($"a > b: {a > b}"); // true
7Console.WriteLine($"a < b: {a < b}"); // false
8Console.WriteLine($"a >= b: {a >= b}"); // true
9Console.WriteLine($"a <= b: {a <= b}"); // false

Common Mistake: = vs ==

csharp
1int x = 5;
2
3// Assignment (sets x to 10)
4x = 10;
5
6// Comparison (checks if x equals 10)
7bool isEqual = x == 10; // true

Comparing Different Types

csharp
1// Integers
2int score1 = 85;
3int score2 = 90;
4bool isHigher = score2 > score1; // true
5
6// Doubles
7double price1 = 19.99;
8double price2 = 20.00;
9bool isCheaper = price1 < price2; // true
10
11// Characters (compared by ASCII value)
12char a = 'A'; // ASCII 65
13char b = 'B'; // ASCII 66
14bool result = a < b; // true

String Comparison

Strings have special comparison rules:

csharp
1string name1 = "Alice";
2string name2 = "Alice";
3string name3 = "alice";
4
5// == compares string content
6Console.WriteLine(name1 == name2); // true
7Console.WriteLine(name1 == name3); // false (case-sensitive)
8
9// Case-insensitive comparison
10bool sameIgnoreCase = string.Equals(name1, name3,
11 StringComparison.OrdinalIgnoreCase); // true
12
13// Comparing with null
14string empty = null;
15Console.WriteLine(name1 == null); // false
16Console.WriteLine(empty == null); // true

String Ordering

csharp
1string a = "apple";
2string b = "banana";
3
4// CompareTo returns: negative if less, 0 if equal, positive if greater
5int comparison = a.CompareTo(b); // negative (a comes before b)
6
7// String.Compare
8int result = string.Compare("ABC", "abc"); // depends on culture
9int resultIgnoreCase = string.Compare("ABC", "abc",
10 StringComparison.OrdinalIgnoreCase); // 0 (equal)

Storing Comparison Results

csharp
1int age = 25;
2int requiredAge = 18;
3
4// Store comparison result in a boolean
5bool isAdult = age >= requiredAge;
6Console.WriteLine($"Is adult: {isAdult}"); // Is adult: True
7
8// Use in expressions
9string 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:

csharp
1int x = 5;
2
3// This does NOT work as expected:
4// bool inRange = 1 < x < 10; // Compiler error
5
6// Correct approach (using logical AND)
7bool inRange = x > 1 && x < 10; // true

Practical Examples

csharp
1// Grade checker
2int score = 85;
3bool isPassing = score >= 60;
4bool isExcellent = score >= 90;
5
6Console.WriteLine($"Passing: {isPassing}"); // true
7Console.WriteLine($"Excellent: {isExcellent}"); // false
8
9// Price comparison
10decimal currentPrice = 49.99m;
11decimal budgetLimit = 50.00m;
12
13bool canAfford = currentPrice <= budgetLimit;
14Console.WriteLine($"Can afford: {canAfford}"); // true
15
16// Temperature check
17double 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.

Assignment and Comparison Operators - Anko Academy