Start learning free

LESSON 67 OF 347 · CONDITIONAL STATEMENTS

Comparison Operators in C#: ==, !=, <, >, <=, >=

Lesson Read the full explanation

When writing if statements, we usually compare two values using one or more comparison operators:

Operator Name Examples
== Equal  x == 5,  x == y
!= Not equal  x != 3,  x != y
> Greater than  x > 5,  x > y
< Less than  x < 8,  x < y
>= Greater than or equal to  x >= 4,  x >= y
<= Less than or equal to  x <= 3,   x <= y

For example:

10 == 10 // Evaluates to true
3 < 2 // Evaluates to false
15 >= 15 // Evaluates to true
17 != 12 // Evaluates to true

We usually use these operators with 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 not satisfied since 10 == 15 evaluates to false.

The second condition is satisfied since 10 < 15 evaluates to true.

The code above prints "a is less than b".


Assignment
Follow the Coding Tutorial and let's play with comparison operators.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    C# · CONDITIONAL STATEMENTS

    Comparison Operators in C#, explained

    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.

    OperatorMeaningExampleResult
    ==equal to10 == 10true
    !=not equal to17 != 12true
    >greater than3 > 2true
    <less than3 < 2false
    >=greater than or equal to15 >= 15true
    <=less than or equal to4 <= 3false

    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 for int variables, because the assignment produces an int, not a bool.
    • For strings, == compares the text, so "abc" == "abc" is true. The comparison is case-sensitive: "Sunny" == "sunny" is false.
    • The ordering operators < and > work on numbers and characters, but not directly on bool values or strings.
    • Comparing an int with a double is allowed; the int is converted first. 5 == 5.0 is true.

    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.

    1. Write the condition. Use grade < 5. The operator is strict, so a grade of exactly 5 does not count as failed.
    2. Print the failure. Inside the if, call Console.WriteLine("Failed the test");.
    3. Handle everything else. Add an else branch that prints Passed 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 <. With grade <= 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 < 10 does not compile in C#, because 0 < x produces a bool, which cannot then be compared with 10 using <. 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 == on double values that come from calculations; compare with a small tolerance instead.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free