What comparison operators do
A comparison operator looks at two values and produces a bool: true or false. That result is what an if statement needs, so comparisons are how a program asks questions about its data.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | equal to | 10 == 10 | true |
!= | not equal to | 17 != 12 | true |
> | greater than | 3 > 2 | true |
< | less than | 3 < 2 | false |
>= | greater than or equal to | 15 >= 15 | true |
<= | less than or equal to | 4 <= 3 | false |
The "or equal to" versions matter at the boundary. 15 > 15 is false because the values are equal, while 15 >= 15 is true.
Using them in an if statement
Comparisons usually involve variables:
int a = 10;
int b = 15;
if (a == b) {
Console.WriteLine("a is equal to b");
}
if (a < b) {
Console.WriteLine("a is less than b");
}
The first condition is 10 == 15, which is false, so its block is skipped. The second is 10 < 15, which is true. The program prints one line:
a is less than b
Because a comparison is just an expression with a bool value, you can also store it: bool isSmaller = a < b;.
C# details worth knowing
=assigns and==compares. They are easy to mix up.if (a = b)does not compile forintvariables, because the assignment produces anint, not abool.- For strings,
==compares the text, so"abc" == "abc"istrue. The comparison is case-sensitive:"Sunny" == "sunny"isfalse. - The ordering operators
<and>work on numbers and characters, but not directly onboolvalues or strings. - Comparing an
intwith adoubleis allowed; theintis converted first.5 == 5.0istrue.
Comparing decimal numbers
Characters compare by their character codes, so 'a' < 'b' is true, and an uppercase letter is smaller than any lowercase letter: 'Z' < 'a' is true. That is worth remembering when you compare letters, not just numbers.
Be careful with == on double values. Decimal fractions such as 0.1 and 0.2 cannot be stored exactly in binary, so small errors creep in. In C#, 0.1 + 0.2 == 0.3 evaluates to false. For measured or computed decimals, check that the difference is tiny instead, for example Math.Abs(0.1 + 0.2 - 0.3) < 1e-9, which is true. Comparisons on whole numbers such as int are exact, so this problem only appears with double and float.
Using the result
Since a comparison is a value, it can feed straight into a variable or, later, into a return. For instance, bool even = n % 2 == 0; stores whether n is even. Here the remainder is computed first, then compared to zero. You will use this shape a lot.
The exercise, step by step
The lesson gives you int grade = 8; and asks you to report whether the test was passed. A grade below 5 is a fail, and anything else is a pass.
- Write the condition. Use
grade < 5. The operator is strict, so a grade of exactly 5 does not count as failed. - Print the failure. Inside the
if, callConsole.WriteLine("Failed the test");. - Handle everything else. Add an
elsebranch that printsPassed the test. It runs for every grade of 5 or more, so you do not need to write a second condition.
int grade = 8;
if (grade < 5) {
Console.WriteLine("Failed the test");
} else {
Console.WriteLine("Passed the test");
}
With grade equal to 8, the condition 8 < 5 is false, so the program prints Passed the test. Try 4, 5 and 6 in the editor to see the boundary: only 4 fails.
Common mistakes
- Using
<=when you mean<. Withgrade <= 5, a grade of 5 would be reported as failed. Decide whether the boundary value belongs on the true side or the false side. - Writing the operator backwards.
=<and=>are not comparison operators. The equals sign comes second:<=,>=. - Chaining comparisons like in maths.
0 < x < 10does not compile in C#, because0 < xproduces abool, which cannot then be compared with10using<. Each comparison stands alone, and you join them with the logical operators covered later in this chapter. - Using
=for comparison. Equality is==, and "not equal" is!=, not<>.
Key takeaways
- Comparison operators produce a
bool. - The six operators are
==,!=,<,>,<=and>=. - Check the boundary value: strict and "or equal" operators differ exactly there.
- In C#,
==on strings compares their contents, and it is case-sensitive. - Avoid
==ondoublevalues that come from calculations; compare with a small tolerance instead.