c sharp

# C# != Inequality Operator

This Linux Hint guide will talk about the inequality operator (!=) in the C# (C Sharp) language. The inequality operator (!=) is a relational operator. The relational operates the relation of two expressions or values. In mathematics, we study inequalities. They demonstrate the relation of those expressions that are not equal. They are six in number, and the inequality operator (!=) is one of those. It illustrates how expressions relate to one another when they are not equal. While making decisions, we employ this operator. They are utilized in decision-making statements like the “if” statement. We can use them anywhere within the loop. But we can only use this operator inside the decision statement to make a decision.

Syntax

Exp_1 != Exp_2 ­

The inequality operator is very helpful when we have to make a decision. The syntax is very easy. Define two expressions say “Exp_1” and “Exp_2”. Between these two expressions, use the inequality operator (!=).

The operator operates by returning true when either of the two expressions is not equal to the other. The name itself says that “inequality” is when two expressions or values are not equal to each other. The condition becomes true, and it executes the body of the decision statement. The decision statement can be if, else-if, or switch. And when either value or expression is equal, the condition becomes false and will not go inside the body of the decision statement.

## Example No. 1

In this code, we will learn how to verify whether the two integer-type numbers are equal:

using System;

class Program_0
{
static void Main() {
int Val_1 = 56;
int Val_2 = 18;

if(Val_1 != Val_2)
{
Console.Write("The sum of values is :  ");
Console.WriteLine(Val_1+Val_2);
}
}
}

First, inside the static main() function, declare two integer type variables. The “Val_1” store the integer value of 56, and “Val_2” stores the integer value of 18. After defining the variables, apply the “if” statement to make a decision. Within the “if”, use both the variables (Val_1 and Val_2) as operands and write the inequality operator (! =). The body of the “if” statement will print a message on the screen by calling the Console.Write() method. After this, it will add both variables and represent the resultant value on the console. The compiler will compile the program line by line. It will compile the first two statements that initialize the variables. It will store the two variables in the memory with their values. Then it will compile the “if” statement. If the defined condition of the “if” statement is true, then its body will be executed. The compiler will get the value of “Val_1” and “Val_2” and compare both these values. Are they not equal? If yes, then it will return true and execute the body of the “if” statement. Whenever it returns false, the compiler simply ignores the body and moves to the next step. When the “if” statement returns true, the body will be executed and display a text with a sum of both values. ## Example No. 2

This instance will simplify the usage of the inequality operator (!=) for the floating point values.

using System;

class Program_1
{
static void Main() {
float Value_1 = 6.9f;
float Value_2 = 8.7f;

if(Value_1 != Value_2)
{
Console.Write("The result of values after division is :  ");
Console.WriteLine(Value_1/Value_2);
}
else{
Console.Write("The result of values after multiplication is :  ");
Console.WriteLine(Value_1*Value_2);
}
}
}

The code starts by initializing two floating point values. In the next statement, utilize an if-else statement to determine whether the two values are equal or not. Within the “if” statement, set the condition, Value_1 != Value_2. In the body of “if”, employ the Console.Write() method to write the message on the console. After this, the method Console.WriteLine() is being called to display the result of two variables after division. If the defined condition is false, then execute the “else” part with two statements to display on the console. But in the “else” part, we will multiply the two variables using the “*”operator. The compiler will execute the “if” statement and evaluate whether both values are identical. If they are equal, then the body of “if” will be implemented, and the first value, “Value_1”, will be divided by the second “Value_2”. If not, then the “else” part will be executed, and the values of “Value_1” and “Value_2” will be multiplied. ## Example No. 3

In this example, we explore the working of the inequality operator (!=) in the “for” loop.

using System;

class Program_2
{
static void Main() {
for(int i=10; i<20; i++)
{
if(i != 19 )
{
Console.WriteLine("The "+i+" is not equal to 19");
}
else{
Console.WriteLine("\nThe "+i+" is  equal to 19");}
}
}
}

After calling the static void Main() function, apply a “for” loop and initialize the iterator. Within the “for” loop, use the “if” statement to check whether the loop values are not equal to 19. And the “else” statement will be implemented when the “if” statement returns false. The “for” loop will iterate ten times, and all nine times, the “if” statement will return true. But only one time will it execute the “else” part because the iterator value and the condition value become equal, which makes the “if” statement false. The body of “if” will print a message with the value of the iterator. Similarly, the body of the “else” statement utilizes the Console.WriteLine() method to show the text. ## Example No. 4

We will utilize the inequality operator within the do-while loop in this program.

using System;

class Program_3
{
static void Main() {
int i= 20;
do
{
i+=10;
if(i != 40)
{
Console.WriteLine("The Number "+i+" is not equal to 40 " );
}
else
Console.WriteLine("The Number "+i+" is equal to 40 " );

}while(i<80);
}
}

The integer type variable will be declared by the value of 20. Next, use a “do-while” loop to loop for a given condition. Inside the “do” part, increment the “i” with 10. Here, set the condition in the “if” statement, which shows that the value of “i” should not be equal to 40. If this condition is satisfied, the body of the “if” statement will run, and if it is not, the “else” statement will run. A message containing the value of “i” can be seen by calling Console.WriteLine() method in the body of the “if” and “else” statements. ## Conclusion

This guide explicitly discusses the inequality operator (!=) and how it is employed in the C# language. To summarize, we learned how to use the inequality operator (!=) with integer values, floating point values, if, if-else, for loop, and do-while loop. This is a relational operator that is applied when we need to acquire that the values are not equal. When they are not equal, they will return true. 