Menu
Coddy logo textTech

Comparison Operators

Part of the Fundamentals section of Coddy's C# journey — lesson 20 of 69.

Comparison operators are used to compare between two operands.

Sometimes we need to check whether an operand is bigger/smaller/... than another operand. The following table shows possible operators for comparison:

OperatorMeaningExample
==Equal1 == 2 returns false
!=Not Equal1 != 2 returns true
>Greater Than1 > 2 returns false
<Lower Than1 < 2 returns true
>=Greater or Equal1 >= 2 returns false
<=Lower or Equal1 <= 2 returns true


The comparison operator returns true if the comparison is correct or false otherwise.

For example:

int var1 = 13;
int var2 = 12;
bool var3 = var1 != var2;

var3 will hold true because var1 and var2 are not equal

Remember the boolean type,  var3 is a boolean.

challenge icon

Challenge

Beginner

Write a script that initializes 2 variables n1 and n2 with the values 8 and 9 (accordingly).

After that initialize another variable n3 that will hold whether n1 is bigger than n2.

Cheat sheet

Comparison operators are used to compare between two operands and return true or false:

OperatorMeaningExample
==Equal1 == 2 returns false
!=Not Equal1 != 2 returns true
>Greater Than1 > 2 returns false
<Lower Than1 < 2 returns true
>=Greater or Equal1 >= 2 returns false
<=Lower or Equal1 <= 2 returns true

Example usage:

int var1 = 13;
int var2 = 12;
bool var3 = var1 != var2; // var3 will hold true

Try it yourself

using System;

public class Program {
    public static void Main(string[] args) {
        // Type your code below
        
        
        // Don't change the line below
        Console.WriteLine("n1 = " + n1 + ", n2 = " + n2 + ", n3 = " + n3);
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals