C# Comparison Operator with Example – Learn to Compare Values
In C#, comparison operators are used to compare two values. These operators return either true
or false
, depending on the comparison. You can use them when you need to make decisions or check conditions in your code. Sounds useful, right?
For example, let’s say you’re building a simple age checker app. You might want to check if a person is eligible to vote, based on their age. So, you’ll need to use comparison operators to check if their age is greater than or equal to 18.
Here are the common C# comparison operators:
==
(Equal to)!=
(Not equal to)>
(Greater than)<
(Less than)>=
(Greater than or equal to)<=
(Less than or equal to)
Let’s jump right into an example to understand it better.
Real-World Example: Age Checker
Imagine you’re at the entrance of a voting booth, and you want to check if someone is eligible to vote. To do that, you compare their age with 18. If the person’s age is greater than or equal to 18, they can vote!
C# Code Example:
using System;
class AgeChecker
{
static void Main()
{
// Input: Enter a person's age
Console.Write("Enter your age: ");
int age = Convert.ToInt32(Console.ReadLine());
// C# comparison operators used for the check
if (age >= 18)
{
Console.WriteLine("You are eligible to vote!");
}
else
{
Console.WriteLine("Sorry, you are not eligible to vote yet.");
}
}
}
Explanation of the Code
Here’s a breakdown of the code:
- User Input: The program first asks the user to enter their age.
- Comparison Operation: It checks if the age is greater than or equal to 18 using the
>=
comparison operator. - Decision: If the condition is
true
(meaning the person is 18 or older), they are eligible to vote, and the program displays a message. Otherwise, it tells them they are not eligible yet.
Example Output:
Let’s say you enter 20 as your age. The output will be:
Enter your age: 20
You are eligible to vote!
But, if you enter 16:
Enter your age: 16
Sorry, you are not eligible to vote yet.
Conclusion:
So, that’s a simple way to use C# comparison operators with an example! Comparison operators are powerful tools that help us make decisions in our programs. Whether you’re checking if someone can vote or comparing numbers, these operators come in handy all the time.
Now you know how to use comparison operators like ==
, !=
, >
, <
, >=
, and <=
. Whether you’re checking ages, prices, or other values, these operators will be there to help you make the right calls. And the best part? It’s super easy once you get the hang of it!
Next time, when you need to compare two values, just remember: C# comparison operators have got your back!
Next What?
Awesome job! Now that you’ve learned how to use C# comparison operators with an example, you’re on the right track. 🎉
But wait—there’s more! In the next lesson, you’ll learn about logical operators. Logical operators let you combine multiple conditions, making your code even more powerful and flexible. You won’t want to miss it, trust me! 😄