---
title: "Comparison Operators in C# Tutorial"
url: https://weworkworldwide.com/tutorials/comparison-operators-in-c-tutorial-2/
description: "Before reading this section, we're assuming that you already read the C# operator section. We do use comparison in real life pretty much every day if not e"
date: 2026-09-01T13:00:03+00:00
source: https://weworkworldwide.com/llms.txt
---

# Comparison Operators in C# Tutorial

Before reading this section, we’re assuming that you already read the *C# operator section*.

We do use comparison in real life pretty much every day if not every hour! For example when we want to buy a phone, we compare multiple brands with each other and the one that attracts us more (price based, design based etc.) we buy that one. Or when we want to buy fruit, we compare their colors, their freshness and choose those that look healthier.

In C# programming we use comparison to compare values as well.

Most of the times we compare the value of a variable or the values of multiple variables with each other and based on the result, decide to run a set of instructions (codes) or to just skip it.

This is where comparison operators can help.

Here’s the list of comparison operators that can be used in C# programming:

| **Operator**             | **Symbol** |
|--------------------------|------------|
| Equal                    | ==         |
| Not equal                | !=         |
| Greater than             | >         |
| Less than                | <         |
| Greater Than or equal to | >=        |
| Less than or equal to    | <=        |

Comparison operators are mostly used within conditional-statements. For example *if-statement*, *while-loop*, *for-loop* etc.

Before jump into these operators and start to explain them, we need to give an introductory explanation about one of these conditional statements so that we can run some example and show you in practical how these operators work.

For this reason, we start with an introduction to the `if` statement.

Note: In this section we will just give you a general view of what this statement does but you’ll learn *the `if` statement* in greater details in later sections.

The meaning that this statement has in real world also applies inside the C# language as well.

For example in real world we use `if` statement to say things like: `if my phone rang, I'll answer`. Similarly in C# we use `if` statement for checking one or more values and based on the values we decide to run part of the program or just skip it.

For example: if the value of a variable that is named `height` is higher than 100 then the message “The height is higher than 100” should be sent to the screen otherwise the message “The height is less than 100” is the one to be sent to the screen.

This is an example where the `if` statement can be used.

Alright let’s see the structure of the `if-statement`:

``` line-numbers
if (condition){
 //body of the if-statement where code is and will be run if the condition is true
 }else{
 //the code here will only run if the condition is not true.
 }
```

-   `if`: To create an `if` statement, we start with the keyword  
    `if`.

-   `()`: The condition that we want to be checked is put in the  
    parentheses after the `if` statement.

This is where the comparison operators are useful.

For example we can use the equal `==` operator and check the value of a variable with another value and if they were equal then the body of the `if` statement runs otherwise the body will be skipped (Basically the C# engine will jump over the body and runs the instructions that come afterward).

-   `{}`: the body of an `if-statement` starts from open brace `{`  
    and ends at closing brace `}`. And within this body, we put the codes  
    and instructions that want to be run if the condition of the  
    `if-statement` is true.

-   `else`: the else keyword comes after the body of the `if`  
    statement and is used to set another set of instructions that only will  
    run if the condition that is set for the `if` statement results false.  
    You can think of the instructions set for the `else` statement as the  
    plan B.

Note: the body of `else-statement` is within the braces that come after the `else` keyword. Also this `else-statement` does not have parentheses to put a new condition.

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int age = 20;
```

if (age > 21)

``` line-numbers
{
Console.WriteLine("Yes the age is greater than 21.");
}
```

else

``` line-numbers
{
Console.WriteLine("No the age is less than 21.");
}
}
}
}
```

Output: No the age is less than 21.

In this example there’s a variable named `age` with the value 20. We then used the `if` statement to see if the value of this variable is higher than 21 via the Bigger Than `>` operator. But because the value is less than 21, the result of the condition set in the `if` statement will return false and the body of the `else` statement will run instead.

Alright, now that we have a general view on how `if-statement` works, let’s jump into comparison-operators and explain them one by one:

Note: For the sake of simplicity, this section will not use complex if statements but as we progress along, we will give more advanced examples of this statement.

``` line-numbers
Equal `==` Operator:
```

The equal `==` operator is used to check if the involved operands are equal or not. If they are equal, the return value will be `true` otherwise `false`.

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int height = 100;
if (height == 100)
{
Console.WriteLine("Yes the height is equal to 100.");
}
```

else

``` line-numbers
{
Console.WriteLine("No the height is not equal to 100.");
}
}
}
}
```

Output:

Yes the height is equal to 100.

In this example, the purpose of the `if` statement is to check whether or not the value of the `height` variable is equal 100.

Here because the value of the `height` variable is actually equal to 100, the condition that is set for the `if` statement will result true and so the body of the `if` statement will run.

Note: either the body of the `if` statement will run or the body of the `else` statement, but not both. So here because the body of the `if` statement ran, the `else` statement is ignored.

This operator is on the contrary to the equal `==` operator. Basically this operator is used to check whether or not the two operands involved are not equal. If they weren’t equal then the result of the comparison is true otherwise false.

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int age = 20;
if (age != 21)
{
Console.WriteLine("The age is not 21! It's actually: " + age);
}
```

else

``` line-numbers
{
Console.WriteLine("The age is 21!");
}
}
}
}
```

Output: The age is not 21! It’s actually: 20

Because the value of the `age` variable is not 21, the result of the condition set for the `if` statement is true and so its body ran.

## Greater Than `>` Operator:

This operator is used to see if the value of the operand on the left side of this operator is larger compared to the operand on the right side and if it was then the result of the comparison is true otherwise the result is false.

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int age = 21;
```

if (age > 21)

``` line-numbers
{
Console.WriteLine("The value of the age is bigger than 21.");
}
```

else

``` line-numbers
{
Console.WriteLine("The value of the age is less than 21.");
}
}
}
}
```

Output:

The value of the age is less than 21.

Note: in this example the value of the `age` variable is exactly 21 which is equal to the value that this variable is compared with.

But because the Bigger-Than `>` operator only returns true if the left operand is larger than the right operand, then the result of this condition is false and the body of the `else` statement is the one to run!

Basically this operator doesn’t care if the two operands are equal in value.

## Less Than `<` Operator:

This operator is on the opposite side of Greater-Than `>` Operator. Which means this operator checks to see if the operand on the left side is lesser compared to the operand on the right side. If it was, then the result is true, otherwise false.

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int age = 210;
```

if (age < 21)

``` line-numbers
{
Console.WriteLine("The value of the age is less than 21.");
}
```

else

``` line-numbers
{
Console.WriteLine("The value of the age is larger than 21.");
}
}
}
}
```

Output: The value of the age is larger than 21.

Because the value of the `age` variable is larger than 21, the result of the `age < 21` expression is false and so the body of the `else` statement is the one to be executed.

## Greater than or Equal To `>=` Operator:

This operator is the combination of equal `==` and Greater-Than `>` operators.

Basically via this operator, the operands are checked for two conditions:

1.  Is the left operand greater than the right operand? If yes then  
    return true.

2.  Is the two operands are equal in values? If yes, then return  
    `true`.

If **both** of these comparisons resulted false, then in that case the final result will be `false` as well.

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int height = 4;
if (height >= 5)
{
Console.WriteLine("The height is more or equal to 5 inches.");
}
```

else

``` line-numbers
{
Console.WriteLine("The height is less than 5 inches.");
}
}
}
}
```

Output: The height is less than 5 inches.

In this example the value of the `height` variable is neither equal to 5 nor higher than 5 and so the result of the condition set in the `if` statement is false and the body of the `else` statement is executed instead.

## Less Than or Equal To `<=` Operator:

``` line-numbers
This operator is the combination of equal `==` and Less-Than `<` operators.
```

Basically via this operator, the operands are checked for two conditions:

1.  Is the left operand lesser than the right operand? If yes then  
    return true.

2.  Is the two operands are equal in values? If yes, then return  
    `true`.

If **both** of these comparisons resulted false, then in that case the final result will be `false` as well.

Example:

``` line-numbers
using System;
```

namespace quickExample

``` line-numbers
{
public class Program
{
public static void Main(string[] args)
{
int height = 5;
if (height <= 5)
{
Console.WriteLine("The height is less or equal to 5 inches.");
}
```

else

``` line-numbers
{
Console.WriteLine("The height is more than 5 inches.");
}
}
}
}
```

Output: **The height is less or equal to 5 inches.**

In this example the value of the `height` variable is equal to the value 5 and so the result of the condition of the `if` statement is true and its body ran.
