Comparison Operators in c#

Comparison Operators in c#

Comparison operators in the C# programming language are used to compare two different user-defined data type program parameter variable values, which provides a Boolean result output (true or false) based on the comparison of two given individual program data type values. Comparison operators in C# programs are used to produce decisions in conditional flow statements (if, while, etc.) operators. C# programmers can use comparison operators for multiple value comparisons or testing, such as ==, !=, >, <, >=, <=, etc. to test or analyze multiple conditions and expressions based on operators.

Comparison Operators in c#

List of popular Comparison Operators in C#.

OperatorComparison operator DescriptionExampleOutput
==Equal to Comparison operator used to check Comparison between two different variable valuesp == qOutput is true if p equals q value
!=Not equal to operator used to check not equality between two separate variablesp! = qOutput is true if p does not equal q variable value
Greater than Comparison operator used to check greater than value between two variablesp > qOutput is true if p is greater than q variable value
Less than Comparison operator used to check variable value is lessp < qOutput is true if p is less than q value
>=Greater than or equal to operator used to check given variable value is greater than or equal top >= qOutput is true if p is greater than or equal to q value
<=Less than or equal to operator used to check given variable value is less than or equal top <= qOutput is true if p is less than or equal to q value

Example of a comparison operator in C#.

using System;

class ComparisonOperators

{

static void Main()

{

int p = 7;

int q = 2;

Console.WriteLine($”to check the equality of p == q – { p == q }”); // Result is – False

Console.WriteLine($”to check the not equality of p != q – {p != q}”); // Result is – True

Console.WriteLine($”to check the greater than value is p > q – { p > q}”); // Result is – True

Console.WriteLine($”to check the less than value is p < q – { p < q}”); // Result is – False

Console.WriteLine($”to check the value is Greater than or equal to value p >= q – { p >= q }”); // Result is True

Console.WriteLine($”to check if the value is less than or equal to the value p <= q – { p <= q }”); // Result is False

}

}

Important elements of C# comparison operators.

In the C# programming language, the equality check operator (==) and assignment (=) are used for different purposes in C# programs. While the equality operator compares two values, the assignment operator assigns a value to a user-defined variable.

Elements of C# comparison operators.

  • == compares two variable values ​​in a user-defined program.
  • = assigns a value to a variable in a user-defined program.

int p = 4; // p variable value assignment

bool isEqual = (p == 4); // Check the p variable’s value for equality, true

using System;

class AssignmentOperator

{

static void Main()

{

int p = 7; // Here, assign the value to the p variable

bool isEqual = (p == 7); // Here, check for equality, true

Console.WriteLine(“The value of the p variable is equal to 7?” + p);

Console.WriteLine(“Is the p variable equal to 7?” + isEqual);

}

}

Comparing strings in the C# programming language.

To avoid case sensitivity issues in the C# programming language, the Equals() function method is used to compare two different string text values.

using System;

class Program

{

static void Main()

{

string text1 = “Vcanhelpsu”;

string text2 = “vcanhelpsu”;

bool output = text1.Equals(text2, StringComparison.OrdinalIgnoreCase);

Console.WriteLine(“The String 1 text is – ” + text1);

Console.WriteLine(“The String 2 text is – ” + text2);

Console.WriteLine(“Are they really equal text or case-insensitive? ” + output);

}

}

Comparison for floating-point type values ​​in the C# programming language.

In the C# programming language, avoid directly comparing floating-point data type variable value numbers in any program due to problems with detail precision output value issues.

double p = 0.3 + 0.7;

Console.WriteLine(Math.Abs(x – 0.10) < 0.11); // True

Example of floating-point type value comparison.

Using System;

class FloatingPoint

{

static void Main()

{

double p = 0.3 + 0.4;

// here it Check if p variable value is approximately equal to 0.7

bool isEqual = Math.Abs(p – 0.7) < 0.8;

Console.WriteLine(“the Value of p floating number is – ” + p);

Console.WriteLine(“Is p variable approximately equal to 0.7? ” + isEqual);

}

}

Leave a Reply