Loading lessons...
Comparison & Logical Operators
Comparison & Logical Operators
Comparison operators compare values and return true or false. Logical operators combine bool results.
Comparison operators
| Operator | Name | Example |
|---|---|---|
== | equal to | x == y |
!= | not equal | x != y |
> | greater than | x > y |
< | less than | x < y |
>= | greater than or equal | x >= y |
<= | less than or equal | x <= y |
int x = 5;
int y = 3;
Console.WriteLine(x > y); // True
Console.WriteLine(x == y); // False
Logical operators
| Operator | Name | Example | ||||
|---|---|---|---|---|---|---|
&& | logical and | x < 5 && x < 10 | ||||
| ` | ` | logical or | `x < 5 | x < 4` | ||
! | logical not | !(x < 5) |
int x = 5;
Console.WriteLine(x > 3 && x < 10); // True (both must be true)
Console.WriteLine(x > 3 || x < 4); // True (at least one true)
Console.WriteLine(!(x > 3)); // False (negates the result)
Note on single & and |
& and | also exist but always evaluate both sides. The doubled forms && and || short-circuit: they skip evaluating the right side if the answer is already known.
TL;DR
- Comparison operators return true/false.
&&needs both sides true;||needs at least one.!flips a boolean.&&and||short-circuit;&and|do not.