.NET 1.1+

# C# Relational Operators

*The eleventh part of the C# Fundamentals tutorial returns to the generic operators by describing the relational operators. These useful operators permit the developer to compare two values and make decisions based upon the result of the comparison.*

## Comparing Values

The relational operators allow you to compare two values. You can determine if the two values are equal or not and can check which of the two values is the larger. All of the relational operations return a Boolean result.

In the case of the 'equal to' and 'not equal to' operations, any two compatible data types can be compared. However, for the *comparison operators* that determine if one value is greater than another, the data type must support the concept of ordering. This includes all of the basic numeric types but not Boolean values as it is not sensible to consider 'true' to be either greater than or less than 'false'.

## Equal To Operator

The *equal to* operator compares two values of any data type and returns a 'true' result if the two operands are the same. The operator symbol for equal to is a double-equals signs (==).

int a = 55; int b = 65; int c = 65; bool result; result = a == b; // result = false result = b == c; // result = true

## Not Equal To Operator

The *not equal to *operator compares two operands and returns true if the two values are different. The operator provides the opposite functionality to the *equal to *operator. The operator symbol for *not equal to* is an exclamation mark and an equals signs (!=).

int a = 55; int b = 65; int c = 65; bool result; result = a != b; // result = true result = b != c; // result = false

## Comparison Operators

The remaining relational operators are known as the *comparison operators*. These allow you to compare two values and determine which is the greater. These operators are available only to data types that support the concept of ordering.

Four comparison operators are available C#. These are *greater than* (>), *less than* (<), *greater than or equal to* (>=) and *less than or equal to* (<=). In each case, the operand to the left of the operator is being compared; the statement *a>b* asks the question, is 'a' greater than 'b'?

The following examples show the use of the four comparison operators.

int a = 55; int b = 65; int c = 65; bool result; result = a > b; // result = false result = a < b; // result = true result = a >= b; // result = false result = a <= b; // result = true result = b > c; // result = false result = b < c; // result = false result = b >= c; // result = true result = b <= c; // result = true

## Conditional Processing

The main use of relational operators is to test a condition and change the flow of the program or the value of a variable accordingly. This is known as *conditional processing* and will be explained in detail later in the tutorial.

## Operator Precedence

We can now add the relational operators to the operator precedence table:

Parentheses Operator |
---|

() |

Increment / Decrement Operators |

++(postfix) --(postfix) ++(prefix) --(prefix) |

Complement Operators |

! ~ |

Basic Arithmetic Operators |

* / % + - |

Bitwise Shift Operators |

<< >> |

Comparison Operators |

< > <= >= |

Equivalence Operators |

== != |

Logic / Bitwise Operators |

& ^ | && || |

Assignment Operator |

= |

Compound Assignment Operators |

*= /= %= += -= >>= <<= &= ^= |= |