You are currently viewing C# Relational Operators

C# Relational Operators

C# is a powerful programming language widely used for developing a variety of applications, from desktop software to web applications and games. One fundamental aspect of programming is the comparison of values, and C# provides a set of six(6) relational operators to perform such comparisons.

The Basics: What are Relational Operators?

Relational operators in C# are symbols that allow you to compare two values. C# offers a set of six(6) relational operators that help programmers make decisions based on the relationship between variables. These operators include:

Equal to (==)

The equal to operator (==) checks if two values are equal. It returns true if the values are equal and false otherwise. Here’s an example:

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int num1 = 5;
        int num2 = 5;

        if (num1 == num2)
        {
            System.Console.WriteLine("num1 is equal to num2");
        }
        else
        {
            System.Console.WriteLine("num1 is not equal to num2");
        }

    }
}

In this example, the condition num1 == num2 evaluates to true, so the message “num1 is equal to num2” is printed to the console.

Not equal to (!=)

The not equal to operator (!=) checks if two values are not equal. It returns true if the values are different and false if they are equal. Here’s an example:

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int num1 = 5;
        int num2 = 10;

        if (num1 != num2)
        {
            System.Console.WriteLine("num1 is not equal to num2");
        }
        else
        {
            System.Console.WriteLine("num1 is equal to num2");
        }

    }
}

In this case, the condition num1 != num2 evaluates to true, so the message “num1 is not equal to num2” is printed.

Greater than (>)

The greater than operator (>) checks if the left operand is greater than the right operand. It returns true if the condition is met and false otherwise. Example:

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int num1 = 15;
        int num2 = 10;

        if (num1 > num2)
        {
            System.Console.WriteLine("num1 is greater than num2");
        }
        else
        {
            System.Console.WriteLine("num1 is not greater than num2");
        }

    }
}

Here, the condition num1 > num2 is true, so the message “num1 is greater than num2” is printed.

Less than (<)

The less than operator (<) checks if the left operand is less than the right operand. It returns true if the condition is met and false otherwise. Example:

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int num1 = 5;
        int num2 = 10;

        if (num1 < num2)
        {
            System.Console.WriteLine("num1 is less than num2");
        }
        else
        {
            System.Console.WriteLine("num1 is not less than num2");
        }

    }
}

In this example, the condition num1 < num2 is true, so the message “num1 is less than num2” is printed.

Greater than or equal to (>=)

The greater than or equal to operator (>=) checks if the left operand is greater than or equal to the right operand. It returns true if the condition is met and false otherwise. Example:

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int num1 = 15;
        int num2 = 10;
        
        if (num1 >= num2)
        {
            System.Console.WriteLine("num1 is greater than or equal to num2");
        }
        else
        {
            System.Console.WriteLine("num1 is less than num2");
        }

    }
}

In this case, the condition num1 >= num2 is true, so the message “num1 is greater than or equal to num2” is printed.

Less than or equal to (<=)

The less than or equal to operator (<=) checks if the left operand is less than or equal to the right operand. It returns true if the condition is met and false otherwise. Example:

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int num1 = 5;
        int num2 = 10;

        if (num1 <= num2)
        {
            System.Console.WriteLine("num1 is less than or equal to num2");
        }
        else
        {
            System.Console.WriteLine("num1 is greater than num2");
        }

    }
}

Here, the condition num1 <= num2 is true, so the message “num1 is less than or equal to num2” is printed.

Combining Relational Operators

Relational operators can be combined to create more complex conditions. For instance, you might want to check if a value is within a specific range. Let’s consider an example where we use the logical AND (&&) operator to check if a number is both greater than 10 and less than 20.

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int number = 15;
        
        if (number > 10 && number < 20)
        {
            System.Console.WriteLine("The number is between 10 and 20");
        }
        else
        {
            System.Console.WriteLine("The number is outside the range of 10 and 20");
        }

    }
}

In this case, since the value of number is 15, the program will output “The number is between 10 and 20.”

You can also use the logical OR (||) operator to check if a value meets at least one of several conditions. For example, checking if a number is either less than 5 or greater than 50:

public class RelationalOperators
{
    public static void Main(string[] args)
    {
        
        int value = 3;

        if (value < 5 || value > 50)
        {
            System.Console.WriteLine("The value is either less than 5 or greater than 50");
        }
        else
        {
            System.Console.WriteLine("The value is between 5 and 50");
        }

    }
}

In this instance, since the value of value is less than 5, the program will output “The value is either less than 5 or greater than 50.”

Conclusion

Understanding and effectively using relational operators is crucial for writing robust and logical C# code. These operators provide the means to compare values and make decisions based on those comparisons, enhancing the flexibility and power of your programs.

In this article, we’ve explored the six relational operators in C#: equal to (==), not equal to (!=), greater than (>), less than (<), greater than or equal to (>=), and less than or equal to (<=). Each operator has its own use cases and can be combined to create more intricate conditions.

Leave a Reply