Loading...

- Access to all 42 C# articles plus all upcoming, new content.
- Save countless hours of confusion and frustration with high-quality, easy-to-follow programming tutorials.
- Unrestricted access to 352 MultiCharts, TradingView, and MultiCharts .NET articles.

Subscriptions start at just $15 per month.

Join today and take your trading code programming to the next level!

Relational operators compare values and result in `true`

or `false`

, like `x > 30`

. Logical operators, on the other hand, compare Boolean values with each other. How can these type of operators be combined to create more complex statements?

## C#’s logical operators and their truth table

Logical operators compare two Boolean expressions or (single) values with *and* (`&&`

and `&`

), *or* (`||`

, `|`

, and `^`

), and *not* (`!`

) operators, leading to a single Boolean `true`

or `false`

value (Liberty & MacDonald, 2009; Sharp, 2013).

The result of logical operators can be summarised as follows (Dorman, 2010):

Values of `x` and `y` | `x && y` | `x || y` | `x ^ y` |
---|---|---|---|

`x` = true, `y` = true | true | true | false |

`x` = true, `y` = false | false | true | true |

`x` = false, `y` = true | false | true | true |

`x` = false, `y` = false | false | false | false |

`|`

operator gives the same result as `||`

and `&`

leads to the same value as `&&`

(e.g., Dorman, 2010). The difference between these operators is that `&&`

and `||`

perform short-circuit evaluation (Liberty & MacDonald, 2009).## Difference between C#’s relational and logical operators

Relational operators compare two values and return a Boolean `true`

or `false`

value depending on the result (Dorman, 2010; Liberty & MacDonald, 2009). These operators test values for equal (`==`

), not equal (`!=`

), greater than (`>`

), greater than or equal to (`>=`

), less than (`<`

), and less than or equal to (`<=`

) (e.g., Stephens, 2014).

Relational and logical operators differ in the following way (see Dorman, 2010): logical operators compare Boolean `true`

and `false`

values (like `myBoolA || myBoolB`

), while relational operators generate these Boolean values (`x > 10`

, for example, returns either `true`

or `false`

depending on the value of `x`

).

## Combining relational and logical operators in C#

Together, relational and logical operators can be combined to test more complex statements. For example:

int x = 10; int y = 25; bool boolA = (x > 5) && (y == 30); bool boolB = ((x != 3) || (y < 10)) && (y >= 25); Console.WriteLine("boolA = {0}, boolB = {1}", boolA, boolB); //> boolA = False, boolB = True

Two integer variables are declared here: `x`

(that’s given a value of 10) and `y`

, which is assigned a value of 25. Then two Boolean variables (`boolA`

and `boolB`

) have their value assigned based on a statement that contains both relational and logical operators.

The first statement, which sets the value of `boolA`

, evaluates if `x`

is greater than (`>`

) 5 and `y`

equal to (`==`

) 30. Both expressions are combined with `&&`

, meaning `boolA`

is only assigned a value of true when both comparisons are true. With `y`

unequal to 30, `y == 30`

returns false and so `boolA`

is set to `false`

.

The second statement that assigns `boolB`

its value has two parts. The first uses the or operator (`||`

) to see if either `x`

is unequal to (`!=`

) 3 *or* `y`

is less than (`<`

) 10. With `x`

begin 10 , `x != 3`

returns true. And since `||`

returns true when one of its values is true, the first part evaluates to true.

The statement’s second part uses the and operator (`&&`

) to combine the first part with evaluating if `y`

is greater than or equal to (`>=`

) 25. Since `y`

is 25, this returns true. And so this second part evaluates to true also, resulting in the `&&`

operator to return true. Accordingly, the value assigned to `boolB`

is `true`

.

`x > 0 && < 100`

. Instead, all relational operators require two operands: one value on its left and one on its right (Dorman, 2010). So the `x`

variable needs to be used twice, like so: `x > 0 && x < 100`

.## Tip: use parentheses to make complex expressions more readable

When relational and logical operators are combined, parentheses help prevent making mistakes like the one mentioned above and also clarify statements (Sharp, 2013). For example, consider the following two statements:

bool boolB = x != 3 || y < 10 && y >= 25; bool boolB = ((x != 3) || (y < 10)) && (y >= 25);

Since the precedence of logical operators is less than those of relational operators (Sharp, 2013), parentheses are optional and both statements generate the same result. But in terms of readability, the second line is much clearer than the first.

For more helpful suggestions, see short-circuit evaluation to prevent exceptions with the `&&`

and `||`

logical operators.

### Summary

Logical operators (`&&`

, `&`

, `||`

, `|`

, `^`

, and `!`

) can be combined with relational operators (`==`

, `!=`

, `>`

, `>=`

, `<`

, and `<=`

) to create more complex statements. Even though parentheses aren’t necessary, they make these statements more readable and lessen the chance of making an error.

**References**

Dorman, S. (2010). *Sams Teach Yourself Visual C# 2010 in 24 Hours*. Indianapolis, IN (USA): Sams/Pearson Education.

Liberty, J. & MacDonald, B. (2009). *Learning C# 3.0: Master the Fundamentals of C# 3.0*. Sebastopol, CA: O’Reilly Media.

Sharp, J. (2013). *Microsoft Visual C# 2013 Step by Step*. Microsoft Press.