Conditional expressions
A separate set of operations represents conditional expressions. Such operations return a logical value, i.e. a value of the bool type: true if the expression is true and false if the expression is false. Such operations include comparison operations and logical operations.
Comparison operations
Comparison operations compare two operands and return a bool value: true if the expression is true and false if the expression is false.
- ==
Compares two operands for equality. If they are equal, the operation returns true; if they are not equal, it returns false:
int a = 10;
int b = 4;
bool c = a == b; // false
- !=
Compares two operands and returns true if the operands are not equal and false if they are.
int a = 10;
int b = 4;
bool c = a != b; // true
bool d = a !=10; // false
- <
The “less than” operation. Returns true if the first operand is less than the second, and false if the first operand is greater than the second:
int a = 10;
int b = 4;
bool c = a < b; // false
- >
The “greater than” operation. Compares two operands and returns true if the first operand is greater than the second, otherwise it returns false:
int a = 10;
int b = 4;
bool c = a > b; // true
bool d = a > 25; // false
- <=
The “less than or equal to” operation. Compares two operands and returns true if the first operand is less than or equal to the second. Otherwise returns false.
int a = 10;
int b = 4;
bool c = a <= b; // false
bool d = a <= 25; // true
- >=
The “greater than or equal to” operation. Compares two operands and returns true if the first operand is greater than or equal to the second, otherwise it returns false:
int a = 10;
int b = 4;
bool c = a >= b; // true
bool d = a >= 25; // false
The <, > <=, >= operations have higher priority than == and !=.
Logical operations
C# also defines logical operators that also return a value of type bool. They take values of type bool as operands. They are usually applied to relations and combine several comparison operations.
- |
A logical addition or logical OR operation. Returns true if at least one of the operands returns true.
bool x1 = (5 > 6) | (4 < 6); // 5 > 6 is false, 4 < 6 is true, so it returns true
bool x2 = (5 > 6) | (4 > 6); // 5 > 6 is false, 4 > 6 is false, so false is returned.
- &
Logical multiplication operation or logical AND. Returns true if both operands are both true at the same time.
bool x1 = (5 > 6) & (4 < 6); // 5 > 6 is false, 4 < 6 is true, so it returns false
bool x2 = (5 < 6) & (4 < 6); // 5 < 6 is true, 4 < 6 is true, so true is returned.
- ||
Logical addition operation. Returns true if at least one of the operands returns true.
bool x1 = (5 > 6) || (4 < 6); // 5 > 6 is false, 4 < 6 is true, so it returns true
bool x2 = (5 > 6) || (4 > 6); // 5 > 6 is false, 4 > 6 is false, so false is returned.
- &&
Logical multiplication operation. Returns true if both operands are both true at the same time.
bool x1 = (5 > 6) && (4 < 6); // 5 > 6 is false, 4 < 6 is true, so it returns false
bool x2 = (5 < 6) && (4 < 6); // 5 < 6 is true, 4 < 6 is true, so it returns true
- !
Logical negation operation. It is performed on one operand and returns true if the operand is false. If the operand is true, the operation returns false:
bool a = true;
bool b = !a; // false
- ^
Exclusive OR operation. Returns true if either the first or second operand (but not simultaneously) is true, otherwise returns false
bool x5 = (5 > 6) ^ (4 < 6); // 5 > 6 is false, 4 < 6 is true, so returns true
bool x6 = (50 > 6) ^ (4 / 2 < 3); // 50 > 6 is true, 4/2 < 3 is true, so false is returned.
s Here we have two pairs of operations | and || (as well as & and &&) performing similar actions, but they are not equivalent.
In the expression z=x|y; both x and y values will be calculated.
In the expression z=x||y;, the value of x will be calculated first, and if it is true, then the calculation of the value of y has no sense, because we already have z equal to true anyway. The y value will be calculated only if x is false
The same applies to the pair of &/&& operations. In the expression z=x&y; both values - x and y - will be evaluated.
In the expression z=x&&&y;, the value of x will be calculated first, and if it is equal to false, then the calculation of the value of y has no sense, because we have z equal to false anyway. The value of y will be calculated only if x is true
That's why ||| and && operations are more convenient in calculations because they reduce the time needed to calculate the value of an expression and thus increase performance. The || and && operations are more suitable for performing bitwise operations on numbers.