Comparison Operators
Comparison operators determine how two values relate to each other, evaluating to a Boolean value: true or false.
| Comparison Operator | Example | Meaning | Result |
|---|---|---|---|
== | 5 == 5 | Is 5 equal to 5? | true |
!= | 5 != 3 | Is 5 not equal to 3? | true |
> | 7 > 4 | Is 7 greater than 4? | true |
< | 2 < 6 | Is 2 less than 6? | true |
>= | 5 >= 5 | Is 5 greater than or equal to 5? | true |
<= | 3 <= 2 | Is 3 less than or equal to 2? | false |
Equality operator ==
Compares two operands. Returns true if they are equal, false otherwise.
| English | C# |
|---|---|
| Did I borrow 14 books at the library? | myBookCount == 14 |
| Is the user's name "Alice"? | userName == "Alice" |
Inequality operator !=
Returns true if the two operands are not equal, false otherwise.
| English | C# |
|---|---|
| I put 10 cookies in my pantry, did my cookie count change? | currentCookieCount != 10 |
| I will go to work so long as it's not my birthday. Will I go to work today? | today != myBirthday |
Greater than >
Returns true if the left operand is greater than the right operand, false otherwise.
| English | C# |
|---|---|
| Is McIntosh the first Canadian to win that many gold medals? | mcIntoshGoldCount > bestCanadaGoldCount |
Greater than or equal >=
Returns true if the left operand is greater than or equal to the right operand, false otherwise.
| English | C# |
|---|---|
| The passing grade is 60, is Josh passing this course? | joshGrade >= 60 |
Less than <
Returns true if the left operand is less than the right operand, false otherwise.
| English | C# |
|---|---|
| Did McIntosh set a new world record? | mcIntoshTime < worldRecordButterfly |
Less than or equal <=
Returns true if the left operand is less than or equal to the right operand, false otherwise.
| English | C# |
|---|---|
| Is the temperature outside below or at the freezing point? | temperature <= 0.0 |
References
Bultena, Sandra and Youmna Badawy. "Integers & Floats." 420-SN1 Notes, John Abbott College, john-abbott-college.github.io/SN1-Notes/topics/04_variables/03_integers_floats/03_integers_floats.html
Buschinelli, Mauricio. "420-1P6 - Week 4" John Abbott College, Fall 2023.