Advertisement



< Prev
Next >



Relational Operators










This operator is also knows an not equal to operator. It compares between the values of its two operands by checking for their inequality. It returns a boolean value true when operands have unequal values otherwise it returns false.




A coding example with != operator


// C# - Example of != relational operator

using System;

class A
{
public static void Main()
{
	//Local variables
	int a = 100, b = 5;
	char ch1 = 'z', ch2 = 'a';
	float f2 = 99.999f;
	double d1 = 50.5, d2 = 99.9999; 
	

	//comparing a character with another character
	if(ch1 != 'z')		
		Console.WriteLine("{0} is not equal  to {1}", ch1, 'z');


	//comparing a character with another character
	if(ch1 != ch2)		
		Console.WriteLine("{0} is not equal  to {1}", ch1, ch2);


	//comparing a double value with a double
	if(d1 != 50.4)		
		Console.WriteLine("{0} is not equal  to 50.4", d1, 50.4);


	//comparing an  int with another double
	if(a != 100.001)
		Console.WriteLine("{0} is greater than equal to 100.001", a, 100.001);


	//comparing an double value with a float
	if(d2 != f2)	
		Console.WriteLine("{0} is not equal  to {1}", d2, f2);


	//comparong a double with a double
	if(d1 != 10.5)	
		Console.WriteLine("{0} is not equal  to {1}", d1, 10.5);


	//comparing an int to float
	if(b != 4.999f)		
		Console.WriteLine("{0} is not equal  to {1}", b, 4.999f);


	//comparing an  int with a double
	if(ch2 != 97)		
		Console.WriteLine("{0} is not equal  to {1}", ch2, 97);
}
}


Output -


z is not equal  to a
50.5 is not equal  to 50.4
100 is greater than equal to 100.001
99.9999 is not equal  to 99.999
50.5 is not equal  to 10.5
5 is not equal  to 4.999

Note : There should never be any space between ! and = of != operator, or a compile error is thrown.



Please share this article -





< Prev
Next >
< C# Compound Assignment Operators
C# Logical Operators >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement