Lesson 3.2

C# Relational Operator – With Programming Examples


Objective

What is Relational operator in C#?
What is the symbol of Relational Operator?
How to use Relational Operator in program?

C# Relational Operator – With Programming Examples

Relational Operator in C# is used for comparing one value with another value. It is also known as Compare Operator and the symbols are =, <>, <, >, <= and >=. The relational operator returns value in yes or no.

Relational Operator Symbol

OperatorDescriptionExample
==It returns true if conditions are equal. if(x == y)
!=It returns true if conditions are not equal. if(x != y)
>It returns true if left condition is greater than right condition. if(x > y)
<It returns true if left condition is lower than right conditionif(x < y)
<=It returns true if left condition is greater than or equal to right conditionif(x >= y)
<=It returns true if left condition is lower than or equal to right conditionif(x <= y)

Programming Example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace relational_operator
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 10, y = 20;

            // equal equal to operator
            if (x == y)
            {
                Console.WriteLine("x is equal to y");
            }
            else if (x != y)
            {
                Console.WriteLine("x is not equal to y");
            }

            if (x > y)
            {
                Console.WriteLine("x is greater than y");
            }
            else if (x < y)
            {
                Console.WriteLine("y is greater than x");
            }

            Console.ReadKey();
            
        }
    }
}

		 

Output

x is not equal to y
y is greater than x
__

Summary

In this chapter you have learned what is Relational operator, their symbol is and how to use it in your c# program. In the next chapter we will learn about Logical Operators in C#.

BackNext