Welcome to The Coding College! In this tutorial, we’ll explore C# Comparison Operators, which are used to compare values in C#. These operators return a Boolean result (true
or false
), making them essential for conditional statements and decision-making in programming.
For more tutorials and in-depth programming content, visit thecodingcollege.com—your ultimate coding companion!
What are Comparison Operators in C#?
Comparison operators in C# are used to compare two values or variables. The result of a comparison is always a Boolean value:
true
(if the condition is satisfied)false
(if the condition is not satisfied)
These operators are commonly used in:
- Conditional statements like
if
,else
, andswitch
- Loops like
for
,while
, anddo-while
- Logical operations for decision-making
List of C# Comparison Operators
Operator | Name | Description | Example | Output |
---|---|---|---|---|
== | Equal to | Checks if two values are equal | 5 == 5 | true |
!= | Not equal to | Checks if two values are not equal | 5 != 3 | true |
> | Greater than | Checks if one value is greater than another | 6 > 3 | true |
< | Less than | Checks if one value is less than another | 3 < 6 | true |
>= | Greater than or equal to | Checks if one value is greater or equal | 6 >= 6 | true |
<= | Less than or equal to | Checks if one value is less or equal | 5 <= 6 | true |
1. Equal To Operator (==
)
The ==
operator checks if two values are equal. If they are, it returns true
; otherwise, it returns false
.
Example:
int a = 10;
int b = 10;
if (a == b)
{
Console.WriteLine("a and b are equal.");
}
else
{
Console.WriteLine("a and b are not equal.");
}
// Output: a and b are equal.
2. Not Equal To Operator (!=
)
The !=
operator checks if two values are not equal. It returns true
if they are different and false
if they are equal.
Example:
int a = 10;
int b = 20;
if (a != b)
{
Console.WriteLine("a and b are not equal.");
}
else
{
Console.WriteLine("a and b are equal.");
}
// Output: a and b are not equal.
3. Greater Than Operator (>
)
The >
operator checks if the value on the left is greater than the value on the right.
Example:
int a = 15;
int b = 10;
if (a > b)
{
Console.WriteLine("a is greater than b.");
}
// Output: a is greater than b.
4. Less Than Operator (<
)
The <
operator checks if the value on the left is less than the value on the right.
Example:
int a = 5;
int b = 10;
if (a < b)
{
Console.WriteLine("a is less than b.");
}
// Output: a is less than b.
5. Greater Than or Equal To Operator (>=
)
The >=
operator checks if the value on the left is greater than or equal to the value on the right.
Example:
int a = 10;
int b = 10;
if (a >= b)
{
Console.WriteLine("a is greater than or equal to b.");
}
// Output: a is greater than or equal to b.
6. Less Than or Equal To Operator (<=
)
The <=
operator checks if the value on the left is less than or equal to the value on the right.
Example:
int a = 5;
int b = 10;
if (a <= b)
{
Console.WriteLine("a is less than or equal to b.");
}
// Output: a is less than or equal to b.
Using Comparison Operators in Conditional Statements
Comparison operators are most commonly used with conditional statements like if
and else
.
Example:
int age = 18;
if (age >= 18)
{
Console.WriteLine("You are eligible to vote.");
}
else
{
Console.WriteLine("You are not eligible to vote.");
}
// Output: You are eligible to vote.
Benefits of Using Comparison Operators
- Decision-Making: Comparison operators allow your program to make decisions based on conditions.
- Flexibility: They can be combined with logical operators for more complex conditions.
- Readability: Simple syntax makes them easy to understand and use.
Practice Problem
Try this small program to practice:
int x = 15;
int y = 20;
if (x == y)
{
Console.WriteLine("x and y are equal.");
}
else if (x > y)
{
Console.WriteLine("x is greater than y.");
}
else
{
Console.WriteLine("x is less than y.");
}
Question: What will be the output when x = 15
and y = 20
?
Conclusion
C# Comparison Operators are fundamental tools for comparing values and enabling decision-making in your programs. Mastering these operators will allow you to write logical and efficient code.
To continue learning and improving your coding skills, visit thecodingcollege.com. Start coding today and unlock your potential! 🚀